I've started making my own game recently but I ran into a problem. When i tried to make my collider script tell my gamemanager script to restart the level (GetComponent<GameManager>().endGame();) I got a error saying:
Nullreferenceexception: object reference not set to an instance of an object
So I add the name of the object that the script is placed on and then i get the error
The name _____ does not exist in the current context
...and i just can't get it to work.
Here is my code: collision:
public class Collision : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "obstacles")
{
Debug.Log("Hello");
Overlooker.GetComponent<GameManager>().endGame();
}
}
}
reset level:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void endGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
I wouldn't be surprised if it was something stupid