- Home /
Unreseolved NullReferenceException: Object reference not set to an instance of an object
I have a Game Object ghostBuddy (with a collider) with script GhostBuddy.cs as here:
using UnityEngine;
using System.Collections;
namespace GameScripts.BalloonBalance {
public class GhostBuddy : MonoBehaviour {
public bool isInsideGhost = false;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.GetComponent<singleCharacter>() || other.gameObject.GetComponent<Character_Bimanual>()) {
isInsideGhost = true;
}
}
}
}
and I am trying to check from the game manager if isInside Ghost is true , by setting:
public GameObject ghostBuddy;
void Awake() {
ghostBuddy = (GameObject)Instantiate (Resources.Load ("Prefabs/GhostBuddy"));
}
void FixedUpdate () {
currentPoolSize = pool.Count;
if (shouldIDraw) {
if ((ghostBuddy.GetComponent<GhostBuddy> () != null)) {
if (ghostBuddy.GetComponent<GhostBuddy ().isInsideGhost)
StartSpawningBigEgg ();
}
}
else if (!shouldIDraw) {
return;
}
}
void StartSpawningBigEgg() {
if (currentPoolSize == 4) {
Spawn (1, new Vector3 (0, 2.0f, 0));
Spawn (1, new Vector3 (0, 3.0f, 0));
Spawn (1, new Vector3 (0, 4.0f, 0));
}
}
where spawn will just spawn an egg. I get an error
NullReferenceException: Object reference not set to an instance of an object GameScripts.BalloonBalance.GhostBuddy.FixedUpdate () (
but couldn't figure out why because I have instatitated everything properly.
Answer by Mltdwn · May 18, 2017 at 03:50 PM
This line looks erroneous:
ghostBuddy.GetComponent<GhostBuddy ().isInsideGhost
shouldn't that be
ghostBuddy.GetComponent<GhostBuddy> ().isInsideGhost
Answer by Yoshinator2 · May 18, 2017 at 03:33 PM
Simple problem. In your inspector, just drag the ghostBuddy gameobject into the correct area. Its missing it in the object which is why youre getting that error.
If that fixed your problem, please accept my answer! If not, feel free to ask more questions :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612122307im_/https://answers.unity.com/themes/thub/images/avi.jpg)