Search Unity

Adjust variable in inactive script

Discussion in 'Scripting' started by rmele09, Aug 6, 2016.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    Can you change a variable of a script that is not in the scene or hierarchy, from another script? I can do this easily when the two scripts are in the scene or hierarchy, but how can I change a variable of a script that is just in my assets? If it is not possible I have a workaround ready, but I am very curious if this is possible?

    Thank you!
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    One way would be by first making the var static, then changing it using an internal static function. For example :

    Code (CSharp):
    1. public static int myVariable;
    2.  
    3. internal static void ChangeMyVariable(int newValue)
    4. {
    5.    myVariable = newValue;
    6. }
    You could access the variable by :
    Code (CSharp):
    1. yourInt = ClassName.myVariable;
    Or change it by:
    Code (CSharp):
    1. ClassName.ChangeMyVariable(100);
     
  3. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    Hey thanks for the quick response. Is this possible with UnityScript?
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    I haven't tried, but most probably.
     
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    Alright I'll try and figure something out, thanks for your feedback I appreciate it.