Search Unity

[C#]Ground checking - problem with script

Discussion in 'Scripting' started by Hadzik, Jul 2, 2015.

  1. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    Hello!
    I just have a problem. I use two scripts - 'ButtonMovement' and 'GroundChecker'. ButtonMovement is assigned to my jump button, and GroundChecker to my Player. The problem is that variable 'grounded' doesn't work in ButtonMovement script [it works in GroundChecker (it shows grounded = true if player hit ground and false if it doesn't)]. Here are the scripts (I am novice programmer ;D):

    ButtonMovement:
    http://wklej.org/id/1750647/

    GroundChecker:
    http://wklej.org/id/1750650/
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're creating a new GroundChecker (aren't you getting a warning for that?) inside ButtonMovement instead of using GetComponent<> to access it.

    Change the top line:
    Code (csharp):
    1.  
    2. GroundChecker check = new GroundChecker ();
    3.  
    to:
    Code (csharp):
    1.  
    2. GroundChecker check;
    3.  
    And change your Start() function:
    Code (csharp):
    1.  
    2. void Start () {
    3.      playerRigidbody = playerObject.GetComponent<Rigidbody2D>();
    4.      anim = gameObject.GetComponent<Animator> ();
    5. }
    6.  
    to:
    Code (csharp):
    1.  
    2. void Start () {
    3.      playerRigidbody = playerObject.GetComponent<Rigidbody2D>();
    4.      anim = gameObject.GetComponent<Animator> ();
    5.      check = gameObject.GetComponent<GroundChecker>();
    6. }
    7.  
     
  3. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    It still doesnt work.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Nothing is going to work properly while you have errors in the console. You need to resolve those.
     
  5. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    Now there are no errors, but still doesn't work.