1. Submit bugs to enter our 5.4 Beta Sweepstakes. Read more about it here.
  2. Unity 5.4 public beta is now available for all users. Grab the build and visit the 5.4 beta forum.
  3. Missing a module? More about the new installer in 5.3 here.
  4. 4.7.1 released for Xcode 7.3 support. Thread here.

How do I access variables from another script?

Discussion in 'Scripting' started by SilverFoxMedia, Jan 10, 2010.

  1. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Messages:
    153
    I've been sitting here all morning searching for a way. I'm using JavaScript and I'd like to be able to access a variable (var) called 'hitPoints' from one script in another script. How do I go about this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,656
  3. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Messages:
    337
    ok you put the following

    Script1.js
    Code (csharp):
    1.  
    2. static var <theVariableYouWantToBeAbleToAccess>;

    Then to use it you just use

    Code (csharp):
    1. Script1.<theVariableYouWantToBeAbleToAccess>;
    From any other script.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,656
    Actually you don't do that. Well, you do if you specifically want to use a static variable, but more often you don't. The usual ways are in the docs I linked to.

    --Eric
     
  5. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Messages:
    337
    ahh ok...

    dont suppose you have any thoughts as to how i solve this bouncing ball problem do u Eric :S?:
    http://forum.unity3d.com/viewtopic.php?t=40969

    thx
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,656
  7. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Messages:
    153
    Thanks, That had just what I needed :D
     
  8. donskyale_412

    donskyale_412

    Joined:
    Sep 18, 2011
    Messages:
    1
    Greetings everyone.,
    I'm a new user of unity and i was working on modeling a pineapple which includes mathematical equations. I made a scene where certain data can be inputted then another scene where the model is shown. I want to get the data from the first scene, where I used a different script, to the new one to scale the model. Can you help me work this out?

    Thank you very much!

    --Donskyale_412--
     
  9. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Messages:
    266
  10. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Messages:
    62
    Hello, i struggle to get this concept of sharing variables for array/classes to work.
    My setup is the following:
    I have a GameObject with a Script attached in one scene (using DontDestroyOnLoad() so its always available).
    In this script i have a class defined and an array of this class.

    In another scene, i try to access this with the following code.

    var MD: MainData = GetComponent(MainData);
    selectionStrings2 = new String[3];
    for (i = 0; i<= 2; i++) { selectionStrings2 = MD.boats.name; }

    The class and array is defined the following way (in the MainData script):
    public class boat {
    var name : String;
    var description : String;
    var speed : float;
    var drag : float;
    var angdrag : float;
    }
    public var boats : boat[];

    Now the problem is that i get a NullReferenceException when trying to access this data-structure.
    I have no problems accessing simple variables in this way, this only happens when i try to access a more complex variable like this.
    I also tried setting both the class and the array to static, while i can access it then without the null reference error it behaves strange (data i write in there isnt the data i get when i read it - not sure whats happening but its defintly not working the way i expect).

    How can i solve this? (i guess i just miss something very basic)

    Edit: Solved the problem. I was missing the following logic :
    var something : GameObject;
    something = GameObject.Find("GameData");
    MD = something.GetComponent(MainData);
    So first finding the gameobject (with the script that contains the data), and calling getcomponent on base of this.
     
    Last edited: Jan 5, 2012
  11. wegaz

    wegaz

    Joined:
    May 19, 2014
    Messages:
    5