Why am I getting this error "Object reference not set to an instance of an object"
Hello,
I have tried to research this in the unity community but cat really find much that matches my situation. I get an error saying "object reference not set to an instance of an object". This is usually because I haven't dropped a prefab on to a public spot in the inspector. The thing is my switch case works from within the script its self, but I don't know if accessing it from a different script is causing the error.
I have a prefab named "Ball". On the prefab is a script, with several public Sprite variables.
public class BallScript : MonoBehaviour {
public Sprite RedBall;
public Sprite OrangeBall;
public Sprite GreenBall;
public Sprite YellowBall;
public Sprite TealBall;
public Sprite PurpleBall;
public Sprite PinkBall;
public Sprite BlueBall;
SpriteRenderer MySprite;
public BallType MyCollor;
// Use this for initialization
void Start ()
{
MySprite = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
BallCollor(BallType.Blue);
}
}
public void TestPrint()
{
Debug.Log("Messege reviced!!!");
}
public void TestCollor(int index)
{
switch(index)
{
case 1:
BallCollor(BallType.Blue);
break;
case 2:
BallCollor(BallType.Green);
break;
case 3:
BallCollor(BallType.Yellow);
break;
default:
break;
}
}
public void BallCollor (BallType collor)
{
switch(collor)
{
case BallType.Red:
MySprite.sprite = RedBall;
break;
case BallType.Orange:
MySprite.sprite = OrangeBall;
break;
case BallType.Green:
MySprite.sprite = GreenBall;
break;
case BallType.Yellow:
MySprite.sprite = YellowBall;
break;
case BallType.Teal:
MySprite.sprite = TealBall;
break;
case BallType.Purple:
MySprite.sprite = PurpleBall;
break;
case BallType.Pink:
MySprite.sprite = PinkBall;
break;
case BallType.Blue:
Debug.Log("BLUE CALLED!!!");
MySprite.sprite = BlueBall;
break;
default:
break;
}
}
}
The input.GetKeyDown statement within the Update function works for the switch case. If I press the RightArrow, any instantiated balls in the scene turn to the blue color so I know that it works.
I then have a gameobject in the scene with a script, that will instantiate a ball prefab with the OnMouseDown function. I then GetComponent the script for the instantiated ball, and attempt to call the BallColor function on the ball. I have tried send message as well. The Debug.log line for the blue color is printed, so I know the switch case works, but I then get the error " Object reference not set to an instance of an object".
public class BallSelectScript : MonoBehaviour {
public GameObject ball;
GameObject ball1;
BallScript ballScript;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
ball1 = Instantiate(ball, transform.position, transform.rotation);
ballScript = ball1.GetComponent<BallScript>();
ballScript.BallColor(BallType.Blue);
//ball1.SendMessage("BallCollor" , BallType.Pink);
//ballScript.TestCollor(1);
}
}
When I double click on the error to take me to the line of code that is the issue, the "Mysprite.sprite = BlueBall;" is what is highlighted.
Does anyone know why this is happening? I can't figure it out.
Thank you for the help.
Answer by Bunny83 · May 27, 2019 at 11:52 PM
Assuming that the inspector screenshot you showed us actually shows the ball prefab that you instantiate in your "BallSelectScript", your issue is pretty simple. You initialize "MySprite" inside the Start callback. Start is called right before the next Update. That means when you instantiate the prefab you immediately call ballScript.BallColor(BallType.Blue);
before the Start method of your newly instantuated object has run. Therefore MySprite is not initialized yet.
Debugging becomes way easier when you actually read the error message carefully. It comes is a detailed stacktrace which tells you exactly in which line of which method of which class that error occurred.
To fix this issue, just rename your "Start" method to "Awake". Awake should always be used for self initialization of an object. Start should be used when you actually need to communicate with other objects at start. This ensures that each object has initialized itself before you access anything from that object. The Awake callback will be run from inside the Instantiate call (assuming the prefab isn't deactivated). So when the Instantiate call returns the Awake method has finished running.