Hi, i wonder how to store a script in a variable. i have different scripts on different prefab gameobjects and i want to choose randomly one of these script and execute a function in it.. Like this: Code (JavaScript): exampleGameobject.GetComponent(example Script).Spawn(new Vector3(spawnPointX, spawnPointY), 1); But i didnt found out how to store "example Script" in a variable, so that i can store different script names in different variables. I tried something like that : Code (JavaScript): Var storeScripts = new Array (); storeScripts [0] = exampleGameobject1.GetComponent(example Script1); storeScripts [1] = exampleGameobject2.GetComponent(example Script2); exampleGameobject1.GetComponent(storeScripts [0]).Spawn(new Vector3(spawnPointX, spawnPointY), 1); But that didnt worked. can somebody tell me how to store a script in a variable?
You mean like : Code (JavaScript): var exampleMonoVar : MonoBehaviour = exampleGameobject1.GetComponent(example Script1); exampleGameobject1.GetComponent(exampleMonoVar).Spawn(new Vector3(spawnPointX, spawnPointY), 1); ? That didnt change anything..
storeScripts[0] is an instance of example Script1 and GetComponent needs the type. While it's ugly as sin you could do this: Code (csharp): GetComponent(storeScripts[0].GetType()).Spawn(....)
Hmm, i dont know what im doing wrong i tried this now: Code (JavaScript): var crabs : GameObject; var chooseRandomScript = new Array (); function Start () { crabs = GameObject.FindGameObjectWithTag("Crabs_Prefab"); chooseRandomScript [0] = crabs.GetComponent(Crabs_Script); } function Update () { crabs.GetComponent(chooseRandomScript[0].GetType()).Spawn(new Vector3(spawnPointX, spawnPointY), 1); } i get the error code: Assets/Game_Control.js(72,78): BCE0019: 'Spawn' is not a member of 'UnityEngine.Component'.
Why you call GetComponent again? Try changing this Code (JavaScript): exampleGameobject1.GetComponent(storeScripts [0]).Spawn(new Vector3(spawnPointX, spawnPointY), 1); in Code (JavaScript): storeScripts [0].Spawn(new Vector3(spawnPointX, spawnPointY), 1);
Fraconte's right - what you're doing doesn't make any sense. In either case, GetComponent(Type t) returns a Component. Use the generic overload or cast to the correct type before trying to call Spawn.
Thanks for your help guys. Im new to that kind of stuff, and it makes me crazy.. I can do what i want i dont get it to work. As you said i tried this now: Code (JavaScript): var chooseRandomInsekt = new Array(); chooseRandomInsect[0] = crabs.GetComponent(Crab_Script); chooseRandomInsect[0].Spawn(new Vector3(spawnPointX, spawnPointY), 1); I think i need some typcasting her? but i have no idea how. i tried to use "GetType()" on several position but that doesnt work eighter. Is there no way to just say: " this is a f... script type" ? It seems like unity always thinks this is a "object" type and not a script
If i use typecasting like : Code (JavaScript): var example : Crab_Script = chooseRandomInsect[0]; it works but the problem ist, that i have different script like: "Snailscript". Therefore i cant use always the same type. With "typeof()" i can get the type of a variable / Array field, but how do i use this type? i tried something like this: Code (JavaScript): var storeType = typeof(chooseRandomInsect [0]); var storeInsectWithType : storeType = chooseRandomInsect [0]; But this seems not legit. How can i set a variable to the type i stored in "storeType" ??
In C# you can derive all your classes from a base class with a virtual method Spawn and override it. Then you can declare a list of the base type and do as you did above. But I don't know how to do that in javascript. Or you can simply use an Interface and use a list of Interface.
Hmm im near to my goal in my script and havent implemented my interface yet. I would just need 1 thing: is it possible to assign a datatype to a variable, when this datatype is stored in another variable? Normally you do something like this to give a variable a datatype : Code (JavaScript): var exampleVar2 : float = 1.2; I want to do this: Code (JavaScript): var storeDatatype = typeof(exampleVar1); So now i have the dataytype "float" stored in the variable "storeDatatype" Now i want to create a new Variable and give her the datatype that i stored in "storeDatatype." For Example: Code (JavaScript): var anotherexampleVar : storeDatatype = 1.2; But this definition is not possible this way. Is it possible in any other way to give " exampleVar1" the datatype stored in "storeDatatype"?