- Home /
Change an instanciated objects components "depending" on type
Hello!
To make this question short.
I have an AI model and it could be an Enemy or a Friend depending on the "Shooter" script. I have one for the enemies and another for the friends. And I have different countries you can play as. So for example if you choose England vs. USA then the enemies would be the USA model with the enemy script but if you choose USA vs. England then the USA Model "object" would have the friend script attached. Any way to fix this without too much "Hard coding"?
Thanks!
I look at this problem and i can think of three things....
switch the models around ins$$anonymous$$d of the components. probably the simplest of ways...
destoy the object and instantiate them as a new setting. destroy them and instantiate them with the new setting, e.g US switches to friends, so they get destroyed and then instantiated with the friends script attached
ins$$anonymous$$d of having two scripts have to settings withing a single script
Answer by Zaeran · Nov 02, 2013 at 03:59 PM
Put in something like this:
if(isFriendly){
gameObject.AddComponent<FriendScript>();
}
else{
gameObject.AddComponent<EnemyScript>();
}
isFriendly is true if their country is selected first, and false if selected second