Below is a very simple test script I made to try out a simple idea I need in a more complicated form for a project, but I can’t get this simple version to work so I’m stuck. What I want to do is simply instantiate a cube when the sphere is in a particular position. Here is the code:
var boxToMake : GameObject;
var boxMade : boolean = false;
var movingSphere : GameObject;
function Update () {
if(movingSphere.transform.position.x < 5){
MakeBox();
}
}
function MakeBox(){
if(boxMade == false){
Instantiate (boxToMake, Vector3.Zero, Quaternion.identity);
boxMade = true;
}
}
I get the ol’ “NUllReferenceException: Not set to instance of object” error.
I’ve dragged on both the cube and sphere to boxToMake and movingSphere respectively in the inspector. I’m sure it’s obvious what I’m doing wrong but I’m kinda stuck and would really appreciate any help.
Thank you!
S
I’m on my iPod ATM but something you could try is getting rid of the instantiate coordinates and use Debug.log to let you know if it worked. it should create it with it’s transformation (location/rotation) all 0
He said he dragged the objects to the variables… but he didn’t say if he dragged a prefab from his project or an existing instance of an object from the scene (the latter would result in an error such as this).
Instantiate() will happily clone a clone. I do this to create copies/icons for a HUD in our game. (Just keep in mind only serializable fields are passed to the clone, nothing private (shallow copy)). Anway, just look at the instance of the object with this script in the scene to know for sure.
I can’t imagine why this would be the issue, considering that error message, but Vector3.zero should have a lowercase ‘z’: I don’t really see anything else wrong. Can you tell us which line is being reported?
the last 2 aren’t entirely necessary but I do it anyways. I just had this problem too and nothing worked, in fact my script worked fine when other people used it. I wound up having to start a new project because my old one was bugged up.
Edit: if the first one turns up false then idk (it should turn up false if your code works). maybe set the instantiate() to a variable? like.