GetComponent<>() function error

I have a little problem with the GetComponent<>() function. I’m using it to access a variable from another script. But when I try to access it this error pops out:
“NullReferenceException: Object reference not set to an instance of an object
EnemyController.OnCollisionEnter2D (UnityEngine.Collision2D coll) (atAssets/Scripts/EnemyController.cs:24)”.
Both scripts:

public class EnemyController : MonoBehaviour {

	GameObject GameController;
	ScoreCount scoreCount;
	
	void Start () 
	{
		GameController = GameObject.Find ("GameController");
		scoreCount = GameController.GetComponent<ScoreCount>();
	}

	void OnCollisionEnter2D(Collision2D coll) 
	{
		scoreCount.score += 5;
		Destroy (coll.gameObject);
		Destroy (gameObject);
	}
}

and

public class ScoreCount : MonoBehaviour {

	public int score = 0;

}

The error is about the “scoreCount.score += 5;” line.

Second script has been attached to “GameController” object. It doesn’t do anything yet, but I’d like to have the score variable in it, as the first object gets deleted. It was working earlier, but it seems that I’ve changed something and I can’t figure out what.

Whats the error? is it that GameController is not set? try Debug.Log(GameController) to check it has been set correctly.

I've added Debug.Log(GameController) and nothing happens. I've tried setting this up using Inspector before but there was no difference.

1 Answer

1

So you have an object named “GameController” with the EnemyController script attached? Make sure then that the ScoreCount script is also attached to that object because it seems that your getting a null reference from your call to GetComponent.

Add this after GetComponent to verify if that’s the case:

if(scoreCount == null)
{
    Debug.Log("did NOT find scoreCount");
}
else
{
    Debug.Log("found scoreCount");
}

and check your output console for results.

Side note: according to the OnCollisionEnter2D method, you are destroying the “GameController” object on collision with something (since you’ve stated that “GameController” contains the EnemyController script), which seems suspicious to me. But I believe your specific error is simply due to not having added the ScoreCount component to your “GameController” object.

Ops, I've edited my post and forget to change that. The EnemyController is attached to my Enemy, ScoreCount is attached to GameController. Yeah, it doesn't find the scoreCount.

Check to see if your "GameController" object is actually ticked as being active (check mark to the left of an object's name).

Yeah, it's active. I've tried to make the GameController public and then define it from inspector again but still nothing has changed.

Hmmm - and you're sure that "GameController" has ScoreCount attached directly to it? Not to one of its children or something? It all seems pretty straightforward and I can't think of any other reason why you'd be getting a null reference from GetComponent() if the object does actually have the script attached. Might try GetComponentInChildren() instead. If it continues to fail then try making your EnemyController.scoreCount field public and assigning it directly in the inspector and see if that works.

I feel very silly right now. The problem was inactive "EnemyController" script. And that totally doesn't make sense, as the script was working in some way (I guess it got turned off after I fixed the real issue). Everything works just fine now. I'll try to not make such stupid mistakes in the future. Thank you for your help.