Here’s the error that I have been receiving:
Object reference not set to an instance of an object
GameOver.Start () (at Assets/Scripts/Game/GameOver.cs:19)
if loadedLevel is not 1 then it’s likely that the error will come from
Debug.Log (scorescript.Score);
because scorescript has not been set to anything. it’s good practice to check for null before using any references - in this case like:
if (scorescript != null)
{
Debug.Log (scorescript.Score);
}
btw, your error message relates to a line 19, which does not exist in the code you posted. did you remove something? it’s important that you post accurate code when it refers to error messages otherwise our ability to help you is limited.
According to your answer to tanoshimi question above your scripts aren’t attached to the same Gameobject. That’s why GetComponent can’t find the component
If you call just
GetComponent<ScoreScript>()
You actually doing:
this.GetComponent<ScoreScript>()
So you call GetComponent on your GameOver script instance. This will only find components which are attached to the same GameObject as your GameOver script is attached to. If they are on different objects, you have to find / get a reference to that GameObject first and use Getcomponent on the found gameobject.
If the scripts are all in the scene, it’s easier to simply drop the GetComponent stuff all together and simply assign the script references in the inspector. For that you have to declare your variables as public and not private.