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

Acces to another method variables c#

Discussion in 'Scripting' started by qiqette, Oct 3, 2016.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    When I do:
    Code (CSharp):
    1.     public GameObject player;
    2.     SpriteRenderer pSR;
    3.  
    4.     float playerSizeY;
    5.  
    6.     void Start()
    7.     {
    8.         SpriteRenderer pSR = player.GetComponent<SpriteRenderer>();
    9.         playerSizeY = pSR.sprite.bounds.extents.y;
    10.     }
    11.     void Update()
    12.     {
    13.         Debug.Log(playerSizeY);
    14.     }
    It gives me the Log fine (but just 1 time)

    But when I try to do this:
    Code (CSharp):
    1.     public GameObject player;
    2.     SpriteRenderer pSR;
    3.  
    4.     float playerSizeY;
    5.  
    6.     void Start()
    7.     {
    8.         SpriteRenderer pSR = player.GetComponent<SpriteRenderer>();
    9.     }
    10.     void Update()
    11.     {
    12.         playerSizeY = pSR.sprite.bounds.extents.y; //error in this line of code
    13.         Debug.Log(playerSizeY);
    14.     }
    It send me that error:
    NullReferenceException: Object reference not set to an instance of an object
    Forces.Update ()
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    You are declaring your variable "SpriteRenderer pSR" twice. Once as a global and once as a local to start.
    Remove the "SpriteRenderer" from the start and just leave the pSR = part.
     
  3. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Then I get this error:
    The name `pSR' does not exist in the current context
     
  4. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Code (CSharp):
    1.     public GameObject player;
    2.     SpriteRenderer pSR = player.GetComponent<SpriteRenderer>();
    3.     float playerSizeY; //A field initializer cannot reference
    4.                  //the nonstatic field, method, or property `Forces.player'
    5.  
    6.     void Start()
    7.     {      
    8.     }
    9.     void Update()
    10.     {
    11.         playerSizeY = pSR.sprite.bounds.extents.y;
    12.         Debug.Log(playerSizeY);
    13.     }
     
  5. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Code (CSharp):
    1.     public GameObject player;
    2.     float playerSizeY;
    3.  
    4.     void Start()
    5.     {
    6.     SpriteRenderer pSR = player.GetComponent<SpriteRenderer>();      
    7.     }
    8.     void Update()
    9.     {
    10.         playerSizeY = pSR.sprite.bounds.extents.y; //The name `pSR' does not exist in the current context
    11.         Debug.Log(playerSizeY);
    12.     }
     
  6. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    When you declare a variable in a method, the variable can only be read in the method.

    you want pSR to be a field of your class.

    You should read some tutorial of the basics of programmation


    Code (CSharp):
    1.     public GameObject player;
    2.     float playerSizeY;
    3.    SpriteRenderer pSR;
    4.     void Start()
    5.     {
    6.     pSR = player.GetComponent<SpriteRenderer>();    
    7.     }
    8.     void Update()
    9.     {
    10.         playerSizeY = pSR.sprite.bounds.extents.y; //The name `pSR' does not exist in the current context
    11.         Debug.Log(playerSizeY);
    12.     }
     
    qiqette likes this.
  7. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    I knew that, but i didn't know how to solve it, anyways, what I am doing is watching tutorials and reading books to learn, and I had this doubt
     
    CrymX likes this.