GetComponent suddenly stopped working

I have this one script which use get component to access another script.

public class GameOver : MonoBehaviour {
	
	private ScoreScript scorescript;
	private BossEnemyScore besscript;

	void Start()
	{
		if (Application.loadedLevel == 1)
		{
			scorescript = GetComponent<ScoreScript>();
		}
		else
		{
			besscript = GetComponent<BossEnemyScore>();
		}
		Debug.Log (scorescript.Score);
	}
//Other stuff
}

The else part works quite perfectly(even the if part used to)…
here’s the script which I’m trying to access.

public class ScoreScript : MonoBehaviour 
{
	private Transform height;
    private Transform Player;

	public GUIText ScoreText;

	[HideInInspector]
	public float Score = 0f;

	private float HighScore;

 void Update()
	{
		if (height.position.y * 10 > Score) 
		{
			Score = Mathf.CeilToInt (height.position.y * 10);
		}
		ScoreText.text = "Score: " + Score;

		HighScore = PlayerPrefs.GetFloat("High Score");

		if (Score >= HighScore)
		{
			PlayerPrefs.SetFloat("High Score", Score);	
			HighScore = PlayerPrefs.GetFloat("High Score");
			Debug.Log("HighScore");
		}
	}

}

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.