- Home /
How do I assign a script to a clone through another script?
I need to assign a script to a cloned missile that will allow it to explode on impact. What is the correct code to do this assignment through another script?
Answer by OperationDogBird · Aug 14, 2012 at 05:23 AM
You can use AddComponent on the newly created clone
var clone=Instantiate(......);
clone.AddComponent(MyScript);
It wouldnt be a variable, you would just type the name of your script there. So if you have a script called $$anonymous$$issleExplosion, you would just type that name in the AddComponent overload.
clone.AddComponent($$anonymous$$issleExplosion);
ah. thanks. It didnt work that way so I just put quotes around it and that fixed the problem.
The list of available scripts should come up while typing, i personally never use the String version because a string takes longer to process than the Component itself. But hey, if it works, it works :)
yeah. ok so now I have my clonescript added to the clone. my next step is to make an explosion through OnCollisionEnter. I'm instantiating an explosion clone but I don't know how to get the explosionPrefab. What should I put here to get the prefab?
var explosionPrefab : Transform = _;
Instantiate(explosionPrefab, transform.position, transform.rotation);