- Home /
Choose one beetwen Many Script
Hello
I have 10 script that do 10 different weapon but with the same main function.
I would like to make a GameObject that choose 1 between those scripts. The problem is, I don't know how to add all those script to my GameObject. I tried interface and heritage, but the inspector don't want me to add them
I would like to make an array of MyWeapon[]
and select randomly one to instantiate it on my player when he drop it.
But I can't find a way to add all those script in the inspector in order to fill the MyWeapon[]
Do I have to create a gameobject for each script ?
Answer by Hellium · Jan 12, 2018 at 08:50 AM
I haven't tested the script, but give it a try :
public void AddRandomScript( GameObject target )
{
System.Type[] types = new System.Type[]
{
typeof( Weapon1Script ),
typeof( Weapon2Script ),
typeof( Weapon3Script )
} ;
System.Type chosenType = types[ Random.Range( 0, types.Length ) ] ;
target.AddComponent(chosenType) ;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612150331im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to set an object reference field in the Inspector 0 Answers
How do I expose a list of interfaces in the inspector? 4 Answers
1prefab and and a scriptPool in inspector 0 Answers
How to expose a field of type Interface in the inspector? 10 Answers
How can I get all the options this guide have in the inspector? 1 Answer