-3

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

MickyD
  • 14,343
  • 6
  • 43
  • 67
  • 2
    Welcome to SO. When posting a question be sure that your question's title _is a brief explanation of the issue you are having_ rather than _"I'm getting a weird error that doesn't feel right"_ which doesn't tell anyone, anything. Posts on SO are not just for the _"now"_ but also for readers in the future who, by using search engines come to SO seeking answers to problems that were solved years before. For this reason it's important that the question's title take on a form that will correlate with search terms. I don't think your title helps anyone today or the future. – MickyD May 14 '22 at 00:52
  • 1
    Could you tell us please the full error for _"The name _____ does not exist in the current context"_? Anyway, this sounds like a simple typo on your part. Be sure you are using Visual Studio, that the Visual Studio package is installed in your Unity project via Package Manager. This should enable Visual Studio Intellisense displaying auto-complete and little squiggles for typos. – MickyD May 14 '22 at 01:00
  • assests/scripts/collisioncs(12.13): error cs 0103: The name Overlooker doesn't exist in the current context – print youtube May 14 '22 at 07:47

0 Answers0