Search Unity

Ground checking - problem with script

Discussion in '2D' 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. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Groundchecker is a monobehaviour (as per it's class definition), and monobehaviours must be attached to a gameObject to work correctly. You create a new Groundchecker that is not attached to a gameObject in your ButtonMovement script.
    Code (CSharp):
    1. // Don't do this
    2. GroundChecker check = new GroundChecker ();
    3.  
    4. // Because of this
    5. public class GroundChecker : MonoBehaviour {...};
    If the two scripts are on the same object, you could try this:
    Code (CSharp):
    1. GroundChecker check = GetComponent<GroundChecker>();
    But a great way to take advantage of unity is setting the references between scripts through the inspector. Use the following declaration in your ButtonMovement class, then go to the unity inspector and drag your object with the groundchecker script into the placeholder.
    Code (CSharp):
    1. [SerializeField]
    2. GroundChecker check = null;
     
  3. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    Thanks for your help, but it still doesn't work.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Gambit is correct. You say that you have GroundChecker attached to your player, so you shouldn't create a new one, rather you want to access the one that already exist on your player. Try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ButtonMovement : TouchManager {
    5.     public enum type {LeftButton, RightButton, JumpButton};
    6.     public type buttonType;
    7.     public GameObject playerObject = null;
    8.     public GUITexture buttonTexture = null;
    9.     Rigidbody2D playerRigidbody = null;
    10.     public float jumpHeight = 0.0f;
    11.     public float moveSpeed = 0.0f;
    12.     private Animator anim;
    13.     GroundChecker check;
    14.  
    15.     void Start () {
    16.         playerRigidbody = playerObject.GetComponent<Rigidbody2D>();
    17.         check = playerObject.GetComponent<GroundChecker>();
    18.         anim = gameObject.GetComponent<Animator> ();
    19.     }
    20.  
    21.     void FixedUpdate (){
    22.        
    23.         anim.SetBool ("Grounded", check.grounded);
    24.     }
    25.  
    26.     void Update () {
    27.         TouchInput (buttonTexture);
    28.    
    29.     }
    30.  
    31.     void OnFirstTouchBegan (){
    32.         if(check.grounded == true){
    33.         switch (buttonType) {
    34.         case type.JumpButton:
    35.             playerRigidbody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    36.             break;
    37.             }
    38.         }
    39.     }
    40.  
    41.     void OnSecoundtTouchBegan (){
    42.         if(check.grounded == true){
    43.             switch (buttonType) {
    44.             case type.JumpButton:
    45.                 playerRigidbody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    46.                 break;
    47.             }
    48.         }
    49.     }
    50.  
    51.     void OnFirstTouch (){
    52.         switch (buttonType) {
    53.  
    54.         case type.LeftButton:
    55.             playerObject.transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
    56.             break;
    57.  
    58.         case type.RightButton:
    59.             playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
    60.             break;
    61.         }
    62.     }
    63.  
    64.     void OnSecoundTouch (){
    65.         switch (buttonType) {
    66.            
    67.         case type.LeftButton:
    68.             playerObject.transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
    69.             break;
    70.            
    71.         case type.RightButton:
    72.             playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
    73.             break;
    74.         }
    75.     }
    76. }
     
    Hadzik likes this.
  5. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    Unfortunately it still doesn't work.
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Have you checked that your GroundChecker script actually detects the ground? Add a Debug.Log and so that you can see that the check value actually changes. Make sure that the code is actually reached and that it returns the expected value.
     
  7. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    GroundChecker works correctly.
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Strange, because my version of the ButtonMovement script, event should get a pointer to the GroundChecker that is attached to the playerObject. Does the event variable get a value in ButtonMovement's Start method? Or is it undefined/null?
     
  9. Hadzik

    Hadzik

    Joined:
    Jul 2, 2015
    Posts:
    10
    Finally it works! I am so stupid... I changed anim = gameObject.GetComponent<Animator>(); to anim = playerObject.GetComponent<Animator>(); and it was all... Thanks for your help!