unity wont let me attatch object to variable - c#

Unity wont let me drag and drop a game object into the variable
heres my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
public class GameManager : Singleton<GameManager>
{
[SerializeField] private Player currentPlayer;
public Player CurrentPlayer
{
get { return currentPlayer; }
}
private void Awake()
{
Assert.IsNotNull(currentPlayer);
}
}

Your public class GameManager : Singleton<GameManager> should be a monobehaviour.
Monobehaviours are the type to inherit from when you to add functionality to a gameObject. You cannot drag whatever to a script that just hold a [SerializeField].
You need to have a Monobehaviour inherited class as a component of a gameobject (meaning that it should be attached to it). There is where you can drag the componenets into the public or the private [SerializeField] fields shown in the editor when you select the correspoding gameobject.

Your script must inherit from Monobehavior for you to be able to do that. And since Singleton is a generic type, that wont work.

Related

Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}

The local function 'OnTriggerEnter2D' is declared but never used

I don't know how to fix it. It's a 2D game. When you collide with the box, it should load/teleport you to the next scene, but it doesn't.
I tried everything on the internet and it didn't work.
using UnityEngine;
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour
{
public void LoadNextLevel()
{
void OnTriggerEnter2D(Collider2D collider)
{
SceneManager.LoadScene(1);
}
}
}
I expect it to teleport me to my next level.
It looks like you nested the OnTriggerEnter2D() function inside of LoadNextLevel()? That's what the Local Function warning is referring to.
Those should be two separate functions, not one within the other. OnTriggerEnter2D() is a function of MonoBehavior; the MonoBehavior (VictoryZone) is what gets notified of the collision.
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour {
public void LoadNextLevel() {
SceneManager.LoadScene(1);
}
void OnTriggerEnter2D(Collider2D collider) {
LoadNextLevel();
}
}
Note: you might also need to check the GameObject associated with collider to make sure that it's a player, and not an enemy or something (if, hypothetically, you had enemies or projectiles or other objects with colliders moving into the victory zone).

Unity 3d script adding functions as trigger on Inspector window

I have defined three functions (OnReticleEnter(),Exit() and hover()) in the script and attached with a game object:
using UnityEngine;
using System.Collections;
using TMPro;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
void OnReticleEnter()
{
gOBJ.SetActive(true);
}
void OnReticleExit()
{
gOBJ.SetActive(false);
}
void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
My problem is I want these functions in the inspector window so that I can define multiple actions like event trigger's pointer enter.
Is there any way I can make these functions available as event trigger on inspector window?

Assign script instead of GameObject in editor?

I'm connecting my GameObjects by assigning them in the editor.
All I need is the script assigned to a GameObject but I always have to do this:
public class GameBoard : MonoBehaviour {
//assigned the gameobject containing the ScoreScript in the editor
public GameObject totalScore;
private ScoreScript totalScoreScript;
void Start() {
totalScoreScript = this.GetComponent<ScoreScript>();
}
}
Is there a better way to achieve this result?
Just use ScoreScript as the public variable. To assign it drag the game object with the script attached as normal via the editor. This also works for builtin components like Transform.
public class GameBoard : MonoBehaviour {
public ScoreScript totalScoreScript;
}

Instantiating prefabs from another script (The prefab you want to instantiate is null)

So I have an enemy spawner with a method to instantiate prefabs working just fine. Simplified:
public class EnemySpawner : MonoBehaviour {
public GameObject EnemyPrefab;
public void setEnemies()
{
Instantiate (EnemyPrefab, enemyPos, rotation);
}
void Start()
{
setEnemies();
}
}
This works fine. But it doesnt work when I call it from a different script:
public class Player : MonoBehaviour {
public EnemySpawner enemyspawner;
void Update(){
if (Input.GetMouseButtonDown (0))
{
enemyspawner= new EnemySpawner();
enemyspawner.setEnemies();
}
}
I keep getting this error:
ArgumentException: The prefab you want to instantiate is null.
What am I doing wrong?
Edit:
So I figured that I could not create a Monobeaviour by using the New keyword .I changed it to:
enemyspawner = gameObject.AddComponent<EnemySpawner> ();
enemyspawner.setEnemies();
But that still wont work.
I just read your comment about changing:
new to enemyspawner = gameObject.AddComponent<EnemySpawner>();
However, you did not properly solve the problem, you only made a workaround that won't work because your prefab, public GameObject EnemyPrefab;, won't be set.
With your new code gameObject.AddComponent<EnemySpawner>(); You are attaching a script to the Player GameObject, this will bring you problems in the future.
My Suggestion
Create an Empty GameObject and attach EnemySpawner to it and name it EnemySpawnerObj.
Then to call the function setEnemies() from another class you do this:
EnemySpawner spwner = GameObject.Find("EnemySpawnerObject").GetComponent<EnemySpawner>();
spwner.setEnemies();
So what you are doing is looking through your scene for the GameObject named EnemySpawnerObject then you get the component named EnemySpawner and then you can call that class instance.

Categories

Resources