- Home /
Remove self from gameObject before instantiate
I am instantiating a new scaled down prefab based on the current gameObject. But, I do not want the current script (the one that is doing the duplicating) to be on that new object.
I have tried to destroy the current script before duplicating the prefab but it still shows up on the new clone, which when doing this recursively means the new objects each have a stack of duplicate scripts.
Hope that makes an ounce of sense, but I am not sure how to make it any clearer.
Answer by Sundar · Oct 09, 2012 at 11:55 PM
Try this
GameObject newClone = Instantiate(...);
Destroy( newClone.GetComponent<theScriptYouDontWant>() )
This will remove your script as and when clones instantiate.
I have tried this, the script gets instantiated before I can remove it.
You need to create another public function say Activate(), use this ins$$anonymous$$d of Start() or Awake(), that is move all codes inside Start() or Awake() into Activate().
In $$anonymous$$asterObject call Activate() after its instantiated, in other clone objects call Destroy(...) to remove the script.