Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Trouble accessing variables from another script

Discussion in 'Editor & General Support' started by PrimeDerektive, Jun 2, 2010.

  1. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    I'm trying something relatively simple, and cannot figure it out.

    I have my player object, with a script called PlayerStatus, that maintains his attributes (like strength, dexterity, intelligence, and health). in a child object of player is his projectileLauncher object. I'm trying to access the attribute variables from PlayerStatus in projectileLauncher.js, but keep getting NullReferenceExceptions no matter what I try.

    Here's a simplified version of the projectileLauncher.js code:

    Code (csharp):
    1.  
    2. private var player : GameObject;
    3. private var strength : int;
    4.  
    5. function Start(){
    6.  
    7.     player = GameObject.FindWithTag("Player");
    8.     var strength = player.GetComponentInChildren(PlayerStatus).strength;
    9.  
    10. }
    11.  
    What am I doing wrong?
     
  2. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    It looks like your code is finding the parent object, but then you are looking for the component in the parent's children. Try this:

    Code (csharp):
    1.  
    2. private var player : GameObject;
    3. private var strength : int;
    4.  
    5. function Start(){
    6.  
    7.     player = GameObject.FindWithTag("Player");
    8.     strength = player.GetComponent("PlayerStatus").strength;
    9.  
    10. }
    11.  
    [/code]
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    Whoops! Sorry, I was never actually using GetComponentInChildren, I tried that as a last ditch effort, and accidentally pasted that into my forum post.

    My code is exactly what you posted, but still giving me a NullReferenceException. To be specific, it says "NullReferenceException: Object reference not set to an instance of an object".
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    Help me Ulognep, you're my only hope!
     
  5. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    Make sure the names are all exactly right. "player" and "Player" are different, for example. So check:
    + Player
    + PlayerStatus
    + strength

    And make sure the tag has been assigned to the player game object.

    Good luck!
     
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    Check, check, and check. Still nothing :(

    And I know that the player var is successfully caching a reference to my Player object, because I'm successfully playing animations later in the script with player.animation.
     
  7. Guru

    Guru

    Joined:
    Apr 30, 2010
    Posts:
    118
    That's an error I am intimately familiar with and it has ALWAYS been a misspelled or case different spelling of what I am trying to access.

    Maybe post a screen shot and all the relevant code so we can see what's going on a little better.
     
  8. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Try splitting this line:-
    Code (csharp):
    1. strength = player.GetComponent("PlayerStatus").strength;
    ...into two lines:-
    Code (csharp):
    1. var status: PlayerStatus = player.GetComponent("PlayerStatus");
    2. strength = status.strength;
    This won't actually solve the problem, but when you get the null reference error, you can tell from the line number which variable is null. This will tell you whether it was the FindWithTag or the GetComponent call that failed. When you've established that, it is much easier to track down what is ultimately causing the error.
     
  9. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    I am completely retarded, and figured this out :)

    my Player object is set up sort of like the FPS Character Controller prefab, where the controller and control script are on the parent, and the graphic (in my case, the animated character model) is a child of the object with the controller/control scheme.

    I had the player model tagged Player (which is why I was able to access the animations), and not the parent object (which had the PlayerStatus script on it). I switched to just finding the go's by name, and am caching references to both the Player and the PlayerModel in Start() now, so I can access both my stat variables and my animations :)
     
unityunity