- Home /
How to remove the GameObject having the Object
I create small halos on click, and I would like to destroy them after some time but somehow I can only destroy the object that I Instantiate - `Light` and this `GameObject` that I create also has `animation`, `halo`; etc.
How can I destroy the `GameObject` having the light component.
Code :
public Light haloLight;
Object newHalo = Instantiate (haloLight, hit.point, transform.rotation);
Destroy (newHalo, 1.5f);
With this I can only destroy the `Light` component
I have also tried
GameObject newHalo = Instantiate (haloLight, hit.point, transform.rotation) as GameObject;
Destroy (newHalo, 1.5f);
Answer by Yokimato · Nov 12, 2012 at 09:55 PM
Once you have the newHalo (Light), you can access the GameObject by saying:
newHalo.gameObject
Try destroying that once.
@Yokimato If I go like this Destroy (newHalo.gameObject, 1.5f) then I get NullReferenceException: Object reference not set to an instance of an object
Ok, I fixed it with using Light ... as Light then I was able to remove it but I have no idea why this particular method worked.
Your answer
Follow this Question
Related Questions
Attach object to avatar bone 1 Answer
Having Parent Origin Follow Child Objects 1 Answer
How to set GameObject's component to an instance of an object 0 Answers
change pivot of parent game object 1 Answer
Building asset bundles and wanting to move game objects from one parent to a newly created parent 1 Answer