- Home /
NullReferenceException help
I am new to Programming in Unity3D (JavaScript) but I have been programming in Java for about a year now and have had experience in other languages, so I consider myself a decent programmer. But recently I have been getting a good amount of errors from Unity that I can not find out. This one has been bothering me the most:
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
jsStart.BuildPins () (at Assets/jsStart.js:74)
jsStart.Start () (at Assets/jsStart.js:29)
I understand what it means, but not WHY I am getting it. Here is the code where the error occurs:
function BuildPins(){
var currentPin : GameObject;
for(var i : int = 1; i <= 10;) {
currentPin = Instantiate(bowlingPin,Vector3(startX,startY,startZ),transform.rotation);
currentPin.transform.position.y = startY;
if(i == 1){
} else if (i >= 2 && i <= 3){
if(i == 2){
currentPin.transform.position.x = secondX;
currentPin.transform.position.z = secondZ;
} else {
currentPin.transform.position.x = pins[i].transform.position.x;
currentPin.transform.position.z = -pins[i].transform.position.z;
}
} else if (i >= 4 && i <= 6){
if(i == 4){
currentPin.transform.position.x = thirdX;
currentPin.transform.position.z = thirdZ;
} else {
currentPin.transform.position.x = pins[i].transform.position.x;
currentPin.transform.position.z = pins[i].transform.position.z - 0.3368112;
}
} else if (i >= 7 && i <= 10){
if(i == 7){
currentPin.transform.position.x = fourthX;
currentPin.transform.position.z = fourthZ;
} else {
currentPin.transform.position.x = pins[i].transform.position.x;
currentPin.transform.position.z = pins[i].transform.position.z - 0.3368112;
}
}
currentPin.transform.rotation.x = 270;
currentPin.transform.rotation.y = 0;
currentPin.transform.rotation.z = 0;
pins[i-1] = currentPin;//ERROR IS HERE
print(i.ToString);
}
}
I am trying to make a function that will basically set up a pyramid of Bowling pins in a specific place without having to do too much hardcoding (hence the for statement and subtraction and adding off positions). I don't even get through it once because the debug does not print any numbers (i.ToString).
Any help will be appreciated. :) Thanks, Brandon
Answer by FTheCloud · Aug 15, 2011 at 12:05 AM
Your problem is with this piece of code:
var currentPin : GameObject;
You haven't assigned a gameobject to the script in the editor.
But I assign it by instantiate dont I? currentPin = Instantiate(bowlingPin,Vector3(startX,startY,startZ),transform.rotation);
I see in your instantiate function your spawning "bowlingPin". So that would mean you must have a variable in the beginning of your script presumably named "var bowlingPin : GameObject;"? If so then that means that you must assign an object to that variable in the inspector. It usually doesn't say "NullReferenceException" unless you haven't assigned something to a variable in the inspector.
I definately have a Prefab gameobject assigned to bowlingPin
Ohhhhh..... I think its the pins variable, you define it at the end of the script but in unity you have to define it before you use it. Try moving it to the top of your script.