Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I access variables from another script?

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

  1. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    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
    Posts:
    32,401
  3. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    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
    Posts:
    32,401
    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
    Posts:
    338
    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
    Posts:
    32,401
  7. SilverFoxMedia

    SilverFoxMedia

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

    donskyale_412

    Joined:
    Sep 18, 2011
    Posts:
    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
    Posts:
    266
  10. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    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
    Posts:
    5