- Home /
Different sons of inheritance or a different parameter in construction
Hi, I'm trying to have a script control when and how an explosive missile explodes. It looks, on collision, for a SphereOverlay of colliders and then looks at the tag of each of them, to see if they have to be damaged, so it can make a sendMessage to each of the targets.
The problem is that I want both some enemies AND my main character to shoot this type of missile. So I don't know what's better:
OPTION A) Inherit from a central explosionMissile and each of the sons of the inheritance would check for the corresponding tag ("enemy" or "player")
OPTION B) Have the same class for both but set a parameter on instantiation (can it be done) and this parameter wouuld be the String of the tag, so it checks differently each time.
Answer by whydoidoit · May 27, 2012 at 10:21 AM
You want to go with option B.
Make a public variable on your script - then after Object.Instantiate, set it to a value you want to use.
Make sure that you do this though. If your prefab is a transform - do
var myNewObject : Transform;
myNewObject = Object.Instantiate(whatever, somePosition, someRotation);
myNewObject.GetComponent(MyScriptName).myProperty = whateverValue;