Search Unity

Reading variables on another GameObject

Discussion in 'Scripting' started by wilhelmscream, Mar 31, 2014.

  1. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Pretty straightforward (or at least it should be) but I've been wrestling with it for quite some time.

    GameObject A (named "Galactic Map") has the following:

    Code (csharp):
    1.  
    2. var fundsCounter : int;
    3.  
    4. function Funding () {
    5.     fundsCounter = Random.Range (1000, 10000);
    6. }
    7.  
    Game Object B ("Planet") needs to be able to read the value of that variable "fundsCounter". GetComponent doesn't work for this (apparently) and Messages can't include variables.

    Any ideas?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I've tried GetComponent (and read that page and everything else I can find) a hundred ways to Sunday. It seems to only work for altering that variable, not simply reading it. The idea is to set that variable (fundsCounter) as the upper spending limit when building units/structures/etc, so I don't need to actually change it.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There is no issue reading it...if you can alter it then logically you must be able to read it. print (someObject.GetComponent(Foo).someVar);. Simple as that.

    --Eric
     
  5. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    So theoretically.....

    Having this in the "MainScreen" script on one GameObject

    Code (csharp):
    1.  
    2. var fundsCounter : int;
    3.  
    4. function Funding () {
    5.     fundsCounter = Random.Range (1000, 10000);
    6. }
    7.  
    and this on the other

    Code (csharp):
    1.  
    2.  
    3. var mainScript : GameObject;
    4.  
    5. function Start () {
    6. print (mainScript.GetComponent(MainScreen).Funding);
    7. }
    8.  
    9.  
    should work..... but.....doesn't.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because Funding is a method that assigns fundsCounter. It doesn't return a value and you're not calling it like a method to begin with.

    So no - theoretically that shouldn't work :)
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, that shouldn't work. Your Funding function doesn't return anything, so you can't use it with print. Also, you have to call functions like this: Funding(). You can either use print(GetComponent(MainScreen).fundsCounter) after calling the Funding function, or you can make the Funding function return fundsCounter.

    --Eric
     
  8. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I get that. Using the name of the variable (fundsCounter) also doesn't work. So..... if it's not the variable, or the method, what's left?
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because fundsCounter is private. Or GetComponent is returning a Component and you have to cast it to the correct type. I'm not sure because I don't use UnityScript. Depends on the error message you're getting.

    The easiest thing to do would be to change mainScript to a MainScreen instead of a GameObject and skip the GetComponent call completely.
     
    Last edited: Mar 31, 2014
  10. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    "fundsCounter is not a member of MainScreen"

    That would be easier if I knew what that meant.


    EDIT: fundsCounter is a public variable.
     
  11. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Has anyone ever gotten GetComponent working for them in MonoScript? Based on what I'm seeing around here (my ?'s and others) there's a ton of questions and no real answers.
     
  12. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    in script A attached to a GameObject:

    Code (csharp):
    1. var icecream : int = 5;
    2.  
    3. function GetIcecream()
    4. {
    5.      return icecream;
    6. }
    in script B where the GameObject is referenced to in the inspector
    Code (csharp):
    1.  
    2. var fridge : GameObject;
    3.  
    4. function DoSomething()
    5. {
    6.      var how_many = fridge.GetComponent(A).GetIcecream();
    7.      Debug.Log("I have " + how_many + "icecreams!" );
    8. }
     
  13. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Please post the latest version of your scripts.
     
  14. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Script A (named FundingGenerator) :

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var funds : int;
    5.  
    6. function Start () {
    7.     Invoke ("Funding", 0);
    8. }
    9.  
    10. function Funding () {
    11.     funds = Random.Range (0, 10);
    12. }
    13.  
    14. function OnGUI () {
    15.     GUI.Label (Rect (100, 100, 100, 20), funds.ToString ());
    16. }
    17.  


    Script B (named "FundingReader"):

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var mainObject : GameObject;
    5. var availableFunds : int;
    6.  
    7. function Start () {
    8.     var availableFunds = mainObject.GetComponent(FundingGenerator).Funding();
    9. }
    10.  
    11. function OnGUI () {
    12.     GUI.Label (Rect (100, 100, 100, 20), availableFunds.ToString ());
    13. }
    14.  

    Script B shows compiler error:

    (6,13) : Invalid declaration type "void"


    I assume something needs to be inside the parentheses here - .Funding(); - but I have no idea what
     
  15. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    The reason you have that error is because Funding doesn't return anything, but you're trying to get an int out of it.

    You have two solutions you can do.

    One, you can access funds directly.
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var mainObject : GameObject;
    5. var availableFunds : int;
    6.  
    7. function Start () {
    8.     var availableFunds = mainObject.GetComponent(FundingGenerator).funds;
    9. }
    10.  
    11. function OnGUI () {
    12.     GUI.Label (Rect (100, 100, 100, 20), availableFunds.ToString ());
    13. }
    14.  
    Or, you can have Funding return a value
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var funds : int;
    5.  
    6. function Funding () {
    7.     funds = Random.Range (0, 10);
    8.     return funds;
    9. }
    10.  
    11. function OnGUI () {
    12.     GUI.Label (Rect (100, 100, 100, 20), funds.ToString ());
    13. }
    14.  
     
  16. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Hmmmm both of those return "null reference exception"...... I dragged the proper GameObject into the inspector slot though. Always one little problem :/
     
  17. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Post the full error.
     
  18. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Nevermind I got that fixed. However..... the two GUI labels are showing different values (using both methods you showed above, the label on script B always shows 0, regardless of what int the other shows.)
     
  19. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Using the second solution it should work. Using the first solution, there's no guarantee which Start method will be called first. You'd need to call Funding in FundingGenerator from the Awake function.
     
  20. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Well dang. The second solution returns two different int's with no logical reason, and the second I'm sure how to accomplish. Much appreciated though, you're the only one who got me past error messages.
     
  21. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You did take out this portion of the code correct?
    Code (csharp):
    1.  
    2. function Start () {
    3.     Invoke ("Funding", 0);
    4. }
    5.  
     
  22. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Nope. I assume that's an important oversight on my part.

    Should I do so regardless of which solution I use?
     
  23. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    For the first solution it would be changed to:
    Code (csharp):
    1.  
    2. function Awake () {
    3.     Funding ();
    4. }
    5.  
    For the second solution it would be removed.
     
  24. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Ok I think I see what you're saying. Running the two scripts like this:

    A -
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var funds : int;
    5.  
    6. function Awake () {
    7.     Funding();
    8. }
    9.  
    10. function Funding () {
    11.     funds = Random.Range (0, 10);
    12. }
    13.  
    14. function OnGUI () {
    15.     GUI.Label (Rect (100, 100, 100, 20), funds.ToString ());
    16. }
    17.  

    B -
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var mainObject : GameObject;
    5. var availableFunds : int;
    6.  
    7. function Start () {
    8.     var availableFunds = mainObject.GetComponent(FundingGenerator).funds;
    9. }
    10.  
    11. function OnGUI () {
    12.     GUI.Label (Rect (100, 120, 100, 20), availableFunds.ToString ());
    13. }
    14.  
    still results in two different int's showing up.
     
  25. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Oops, a little oversight.

    You're creating a new variable in your Start function in the second script.

    Remove 'var' from line 7 and it will work.
     
  26. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Thank you, sir, I am indeed much in your debt. It works to perfection.