- Home /
Storing a game object in a variable at runtime.
How can I store a game object in a variable, so I could later change another game object into the one stored, like this
Gameobject gb1; //gameobject to change into
Gameobject gb2 = //find the game object to be changed
Start()
{
gb2 = gb1;
}
Answer by Sildaekar · Jan 14, 2014 at 03:55 AM
I'm afraid it's not that simple to "transform" a gameobject into another. You would have to adjust/specify all the components as well. Take a look at the Instantiate Documentation, this should be able to accomplish basically what you were wanting to do.
Steps:
Store the gb1 in a variable
When you want to switch the GameObjects, destroy gb2
Instantiate gb1 in gb2's location
If this answers your questions please don't forget to mark this answer :)
Your answer
Follow this Question
Related Questions
Building asset bundles and wanting to move game objects from one parent to a newly created parent 1 Answer
How to change material of gameobject using C# to a material asset. 2 Answers
Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate? 2 Answers
Changing or replace objects 2 Answers
Change Target from Script in GameObject with another Script 1 Answer