- Home /
Destroying a Transform Component
I am new to Unity and am using a code developed by someone else initially made over two years ago. He had the code destroying a transform component, which previously worked, but now the version 3.4.2 I am using does not allow this transform to be destroyed. Has this changed in a later version?
Further to this I have one gameobject (a fire event) which has many transforms created from it. As I change the position of a wall it collides with I want to destroy the old transform (or clone) and recreate a new one with the correct conditions. Can I destroy this clone / transform in the script as can be done in the inspector by a right click and delete? Or can I hide it in the background or even adapt the transform to the new conditions?
Answer by Sisso · Sep 20, 2012 at 11:20 AM
A GameObject has many Components, one of them is Transformation. So, if you want to destroy something, aways destroy its GameObject.
// my transform reference
var trans : Transform = ...
// I must pass it's gameObject to destroy my "transformation"
Destroy(trans.gameObject)
About your another question. Yes, you could destroy theses "clones", but first you must know how to access. Take a look on these links:
GameObject has static methods that you can search for Objects:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html
Transform has methods to navigate between parents and children:
http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
More reference: http://docs.unity3d.com/Documentation/ScriptReference/index.html
Thanks for your reply.
Has this changed with deleting a transform by itself and not having to delete the game object? I ask that as this code worked previously. I will check out those links.
I am not a old unity user, so I didn't know if it works before.
You can use Destroy to remove GameObject components. But I think that they remove to force every GameObject have always a transformation.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html?from=GameObject
Cheers. I have tried the Destroy(trans.gameObject) which destroys all transforms and the game object which then won't allow me to instantiate any future transforms I need. So I just need to destroy or change the transform by itself, but leave the game object with the code in its current form.
You cannot destroy the transform component of a game object.
All sorted. Your answer helped me identify the issues. Some of what had been identified as transforms should have been game objects. This then required destroying a clone as well. Thanks for your help.
Your answer
Follow this Question
Related Questions
Destroy Child of Game Object 2 Answers
How do I duplicate an object to work with a shootemup 0 Answers
How to destroy more than one clone at runtime??? 1 Answer
Component Disabling vs. Destroying 1 Answer