Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Accesing A Variable From Another Script.

Discussion in 'Scripting' started by Heu, Feb 13, 2012.

  1. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Hello, i was wondering which "code" would i use to Access another Variable from a different script.

    Let's say i would like to access my "Score" from a script and put it's Integer and use it in another.

    Thanks In Advance.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    var scoreScript = GetComponent(scriptPlayer);


    function OnGUI()
    {
    var scoreScript = GetComponent(scriptPlayer);
    GUI.Label(Rect(100,100,100,100),"Your Score Was"+scoreScript.myScore);
    }

    This gives me an error. Does this not work?
    HTML:
    ArgumentException: You are not allowed to call GetComponent when declaring a variable.
    Move it to the line after without a variable declaration.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    That error message tells you exactly how to fix it, though. :p

    Move the "GetComponent(scriptPlayer)" onto a different line. I'm not a JavaScript person, but I'd guess that it's suggesting something like this:
    Code (csharp):
    1. var scoreScript;
    2. scoreScript = GetComponent(scriptPlayer);
    Moreover, it's also saying that you should only call GetComponent in Start() or Awake() if possible. So you should declarethe variable outside of a function, initialise it in Start() or Awake(), and then use it in OnGUI().

    How experienced a coder are you?
     
  5. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    I would have to say Im a beginner, and I understand the foundations of coding in Unity.
     
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    o rly?
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Hehe. I'm pretty sure GetComponent is a foundation of coding in Unity ;)
     
  8. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Well if it is then let it be, I'm asking for help. Not asking being laughed at.
     
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    We're just picking apart your statement, don't read too far into it :) We all started somewhere.

    Your question has already been answered, unless you have another.
     
  10. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Okay, my next one is will this work if one script is in another scene as the other script is in another scene.
     
  11. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You can only access script instances in the same scene. It sounds like you want to use singletons (Google for more info) which allow you to access it from anywhere.

    You also have the choice of saving to disk (depending on what you're trying to do). PlayerPrefs are the simplest way to save small amounts of data.
     
  12. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Okay well theres my problem. Alright Thanks Dman :)