Unity Community


Results 1 to 10 of 10

  1. Location
    Australia
    Posts
    153

    How do I access variables from another script?

    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. Volunteer Moderator
    Posts
    23,678

    Re: How do I access variables from another script?



  3. Posts
    314
    ok you put the following

    Script1.js
    Code:  
    1. static var <theVariableYouWantToBeAbleToAccess>;


    Then to use it you just use

    Code:  
    1. Script1.<theVariableYouWantToBeAbleToAccess>;

    From any other script.

  4. Volunteer Moderator
    Posts
    23,678
    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. Posts
    314
    Quote Originally Posted by Eric5h5
    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
    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. Volunteer Moderator
    Posts
    23,678
    Quote Originally Posted by Hilm
    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
    Sorry, my vector-fu is weak.

    --Eric


  7. Location
    Australia
    Posts
    153

    Re: How do I access variables from another script?

    Quote Originally Posted by Eric5h5
    http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

    --Eric
    Thanks, That had just what I needed


  8. 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. Location
    Germany
    Posts
    266
    Have a look at "DontDestroyOnLoad()", that way your object with the data inputs will still be available in the second scene.

    Documentation:
    http://unity3d.com/support/documenta...royOnLoad.html


  10. Posts
    33
    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[i] = MD.boats[i].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 by abhuva; 01-05-2012 at 09:25 AM. Reason: Solved the problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •