- Home /
Game Object seems empty after instantiation
Hello, I am creating a new Game Object from a prefab, with Instantiate() method. However, whenever try to access a property of the object I created, it seems to be null. So for example what is the best way to create a instantiate a gameobject and change its name to something else right after. My code is here:
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
spawnPos = new Vector3(brickPiece.renderer.bounds.size.x*x,brickPiece.renderer.bounds.size.y*y,0);
GameObject newObj = new GameObject();
newObj = (Instantiate( brickPiece, spawnPos, Quaternion.identity)) as GameObject;
newObj.name = "brick "+(5*y+x); // can not change cuz newObj seems null
}
}
For one thing, you're creating two gameObjects with this code. The line
GameObject newObj = new GameObject();
instantiates an empty game object in the scene.
Try
GameObject newObj = (Instantiate( brickPiece, spawnPos, Quaternion.identity)) as GameObject;
ins$$anonymous$$d.
Now on to your main problem: can you paste the specific error message that you're getting when you try to set newObj.name?
yes you are right. I removed that line and made it exatcly as you said. Now, whenever I try to do newObj.name, or anything else, it says : "NullReferenceException: Object reference not set to an instance of an object."
I somehow got over the problem by defining a 'var' ins$$anonymous$$d of a 'GameObject' on that line. However, a 'var' doesnt have the same properties with GameObject that I can work with.
Answer by Julien-Lynge · Dec 28, 2011 at 04:26 PM
The 'var' workaround is definitely a clue - we know your expression is returning something, the only question is what. You can use the following to print out the type:
Debug.Log(newObj.GetType());
Just put that immediately above the newObj.name assignment. I copied your code and ran it, and it worked without error on my end. The only thing I can think of is that there is something amiss with brickPiece. Can you try replacing brickPiece in the Instantiate with 'new GameObject()' and see if that works?
Also, just as a test, make sure you use 'Object.Instantiate' explicitly so you're not accidentally calling some other Instantiate function. According to the 'as' keyword definition (http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.71%29.aspx), 'as' will return null if the type can't be converted. If you want to more explicitly force the conversion, you can use
GameObject newObj = (GameObject)Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);
That will throw an error rather than silently returning null if the type can't be converted.
I checked the type and it returned a transform object. So i casted the var object to a Transform and was able to access and use all the properties I need.
When I tried
GameObject newObj = (GameObject)Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);
it threw a InvalidCastException and Unity crashed inexplicably. I dont even try to cast newObj to anything else. Weird.. But my problem is solved by casting 'var' to 'Transform' actually. Not sure if its a good thing to do:)But Thank you!
Not sure why you're having issues casting from Transform to GameObject. You can always access the gameObject through the transform. So if you have
Transform newTrans = Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);
you can use
GameObject newObj = newTrans.gameObject;
or get it directly from the instantiate:
GameObject newObj = (Object.Instantiate( brickPiece, spawnPos, Quaternion.identity)).gameObject;