- Home /
Do not destroy a transform
I'm using UnityScript (Javascript) and I would like to know do you have to destroy a transform? I would like to keep my transform active, but also remove it from the scene and then spawn another. When I do that I get a missingreference exception. So, I'd just like to know do you have to destroy or is there a way to redeclare it?
no u cant do that after destroying the game object, transform will get deleted ins$$anonymous$$d you can save the position rotation and scale in different variables and set it back to another object by creating again.
or simply you can disable the object and enable it again, ins$$anonymous$$d of destroying and creating again.
Answer by robertbu · Feb 26, 2014 at 06:56 AM
Transforms only exist as a component of a Game Object, so if you destroy the game object, the transform is gone as well. So you have a few choices. First you could disable the game object rather than destroy it. Any reference to the transform would remain valid. Another option is to save the information from the transform. Assuming you are saving just the one specific transform and not any parents or children, there are only three values you need to save:
transform.position
transform.rotation
transform.localScale
You can save them separately or create your own class and store them. Once you Instantiate() a new game object, just assign the three values, and the new object will have the old object's position, rotation and scale.
I'm not sure of your game mechanic, but you may also have the option of reuse. That is, instead of destroying the game object, just deactivate and reactivate it when you need the object later.
Thanks. I actually did something fairly similar. Ins$$anonymous$$d of making it a transform I just declared it in the code so it doesn't kill it every time, and will look for the name.
Your answer
Follow this Question
Related Questions
Destroy script help? 1 Answer
Confused about GameObject and Transform 1 Answer
JS file deleting multiple game objects 0 Answers
Destroying a Transform Component 1 Answer
Best Way to make a character move 1 Answer