- Home /
Object reference not set to an instance of an object.
I am attempting to create a board game , and my dice script has suddenly stopped working: First i have this:
public GameObject dice;
Die_d6 diceScript;
which i linked in the inspector , and then i wrote this in my start script:
Die_d6 diceScript = (Die_d6)dice.GetComponent(typeof(Die_d6));
And the error lies when i try to access a public variable from that class:
dicenum = diceScript.value;
The annoying thing is that is was working perfectly at oone point and i cant work out what changed. any thoughts?
If everything you specify here is correct, then the only way I can see you get this error is if the game object you initialized 'dice' to does not have a 'Die_d6' script attached.
That is all correct , but i have just noticed that on the gameobject im trying to access , the name of the component has an extra space character in it , but the script beneath dosnt.
In C#, the name of the script and the class name must match exactly. In fact, you shouldn't even be able to attach the script to a game object if they do not match. And if the class name was different than what you are using here, you'd get a compile time error.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Roll a Ball tutorial 1 Answer
Converting from js to c# 1 Answer
what should I do whit this script to work? 2 Answers
TiltShift post effect generates compile error 0 Answers
Script only works onetime 0 Answers