Search Unity

How to assign a script in C# ?

Discussion in 'Scripting' started by NutellaDaddy, Jan 4, 2014.

  1. NutellaDaddy

    NutellaDaddy

    Joined:
    Oct 22, 2013
    Posts:
    288
    I need to assign a public script to a variable in my script. How would I do this?
     
  2. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Can you please give more details? What you said was sort of vague. I may be wrong, but are you asking to get a value (via a variable) from another script? Or call on a function from another script and return that value to the current script?
     
  3. NutellaDaddy

    NutellaDaddy

    Joined:
    Oct 22, 2013
    Posts:
    288
    I'm asking how could I assign a script to a variable in C#. I need it so that the script won't activate until about 5 seconds after I run the scene.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you want it to activate 5 seconds, use a start menu function.
    Code (csharp):
    1.  
    2. void Start(){
    3. invoke("scriptStart",5);
    4. }
    5. void scriptStart(){
    6. this.enabled = true;
    7. }
    8.  
    disable the script in the editor
    That assumes the start function works on a disabled script. If it doesn't then you'd have to do something else.
     
    Last edited: Jan 4, 2014
  5. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    If the above doesn't work (which is unlikely), there are a few things I can think of (like having another object - like the Main Camera - instantiate the object, which in turn would also have the script or maybe a component can be enabled from another script) but all of this actually seems over the top. But I can't judge that at all without even knowing what you are trying to accomplish.

    Why do you need to wait 5 seconds to start an entire script? Maybe I can help suggest a better alternative.