Hey guys. Can someone please tell my why this line is giving me an unassigned variable error? GameObject tile = Instantiate(tile, pos, rotation) as GameObject;
I’m fairly certain it has something to do with the way Instantiate works. I tried formatting it on separate lines like this piece of example code from the Unity tutorials but that give me the same error.
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
Thanks in advance.
what is the exact error message?
also, it looks like you are trying to create tile & instantiate it at the same time. The rotation looks wrong as well, what rotation? the game object that is the prefab’ rotation?
it may not be the best way but when i’m building a prototype where i may need to change things when i get new models or move spawnpoints around etc I make the gameobject public so I can drag the prefab onto it in the inspector & tend to use empty gameobjects as spawnpoints. That way my instantiate has (name of gameobject prefab, position of spawn point, rotation of spawnpoint), something like (I have no pc to double check syntax & my brain is on holiday mode atm)
Instantiate(tilePrefab, spawnPoint.position, spawnPoint.rotation);
Assets/Scripts/TileGenerator.cs(71,52): error CS0165: Use of unassigned local variable `tile’
Is the exact error message.
rotation is a Quaternion that is made in code that you can’t see.
tile is a prefab that I have a reference of through the inspector
pos is a Vector3 that is created and assigned in other code that isn’t shown
What do you mean by “trying to create tile & instantiate it at the same time”? Cant you declare a variable and assign it in the same line? Trying the declaration and assignment in separate lines yielded the same error to.
I don’t think I can make use of your spawn point example for my current project.
you’re saying that the gameobject tile is equal to the instantiation of something caused tile.
tile doesn’t exist at the point you are trying to instantiate it. Try splitting it into 2 actions & assigning the gameobject.
As a test, split it like i mentioned above to see if that works for you
Oh, you mean that ’ tile ’ is already assigned. This line below works.
GameObject t = Instantiate(tile, pos, rotation) as GameObject;
Thanks for the help.