Search Unity

Store a Script in a variable

Discussion in 'Scripting' started by Joralin, Aug 29, 2014.

  1. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    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):
    1. 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):
    1. Var storeScripts  = new Array ();
    2. storeScripts [0] = exampleGameobject1.GetComponent(example Script1);
    3. storeScripts [1] = exampleGameobject2.GetComponent(example Script2);
    4.  
    5. 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?
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Try setting the variable type to MonoBehaviour...
     
  3. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    You mean like :

    Code (JavaScript):
    1. var exampleMonoVar : MonoBehaviour = exampleGameobject1.GetComponent(example Script1);
    2.  
    3. exampleGameobject1.GetComponent(exampleMonoVar).Spawn(new Vector3(spawnPointX, spawnPointY), 1);
    ?

    That didnt change anything..
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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):
    1.  
    2. GetComponent(storeScripts[0].GetType()).Spawn(....)
    3.  
     
  5. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Hmm, i dont know what im doing wrong i tried this now:


    Code (JavaScript):
    1. var crabs : GameObject;
    2. var chooseRandomScript = new Array ();
    3.  
    4. function Start ()
    5. {
    6. crabs = GameObject.FindGameObjectWithTag("Crabs_Prefab");
    7.  
    8. chooseRandomScript [0] = crabs.GetComponent(Crabs_Script);
    9. }
    10.  
    11. function Update ()
    12. {
    13. crabs.GetComponent(chooseRandomScript[0].GetType()).Spawn(new Vector3(spawnPointX, spawnPointY), 1);
    14. }
    i get the error code:

    Assets/Game_Control.js(72,78): BCE0019: 'Spawn' is not a member of 'UnityEngine.Component'.
     
    Last edited: Aug 29, 2014
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Why you call GetComponent again?
    Try changing this
    Code (JavaScript):
    1. exampleGameobject1.GetComponent(storeScripts [0]).Spawn(new Vector3(spawnPointX, spawnPointY), 1);
    in
    Code (JavaScript):
    1.  
    2. storeScripts [0].Spawn(new Vector3(spawnPointX, spawnPointY), 1);
    3.  
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  8. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    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):
    1. var chooseRandomInsekt = new Array();
    2.  
    3. chooseRandomInsect[0] = crabs.GetComponent(Crab_Script);
    4.  
    5. 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
     
  9. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    If i use typecasting like :

    Code (JavaScript):
    1. 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):
    1. var storeType = typeof(chooseRandomInsect [0]);
    2.          
    3. var storeInsectWithType : storeType = chooseRandomInsect [0];
    But this seems not legit. How can i set a variable to the type i stored in "storeType" ??
     
  10. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    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.
     
    Last edited: Aug 30, 2014
  11. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    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):
    1. var exampleVar2  : float = 1.2;
    I want to do this:

    Code (JavaScript):
    1. 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):
    1. var anotherexampleVar : storeDatatype = 1.2;
    2.  
    3. But this definition is not possible this way.
    Is it possible in any other way to give " exampleVar1" the datatype stored in "storeDatatype"?
     
    Last edited: Aug 30, 2014