Search Unity

bool var stops being evaluated after transition edited (animator)

Discussion in '2D' started by fonko, Jun 27, 2018.

  1. fonko

    fonko

    Joined:
    Mar 20, 2014
    Posts:
    23
    So, i have been trying to find out why this is happening.. for like 3 hours now.

    The issue: isGrounded bool stops working after i edit a transition arrow in the animator

    my code is very simple:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public int speed;
    10.     public int jumpSpeed;
    11.  
    12.     public Rigidbody2D myRigidbody;
    13.  
    14.     public bool isGrounded;
    15.     public float groundCheckRadious;
    16.     public LayerMask whatIsGround;
    17.     public Transform GroundCheck;
    18.  
    19.     public Animator myAnims;
    20.  
    21.     private void Start()
    22.     {
    23.         myAnims = GetComponent<Animator>();
    24.         speed = 10;
    25.         jumpSpeed = 8;
    26.         myRigidbody = GetComponent<Rigidbody2D>();
    27.         GroundCheck = GameObject.Find("GroundCheck").transform;
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         isGrounded = Physics2D.OverlapCircle(GroundCheck.position, groundCheckRadious, whatIsGround);
    33.  
    34.         if (Input.GetAxisRaw("Horizontal") > 0)
    35.         {
    36.             myRigidbody.velocity = new Vector2(speed, myRigidbody.velocity.y);
    37.         }
    38.         if (Input.GetAxisRaw("Horizontal") == 0)
    39.         {
    40.             myRigidbody.velocity = new Vector2(0, myRigidbody.velocity.y);
    41.  
    42.         }
    43.         if (Input.GetAxisRaw("Horizontal") < 0)
    44.         {
    45.             myRigidbody.velocity = new Vector2(-speed, myRigidbody.velocity.y);
    46.  
    47.         }
    48.  
    49.         if (Input.GetButtonDown("Jump"))
    50.         {
    51.             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpSpeed);
    52.  
    53.         }
    54.  
    55.         myAnims.SetBool("grounded", isGrounded);
    56.         myAnims.SetFloat("speed", Mathf.Abs(myRigidbody.velocity.x));
    57.         Debug.Log(isGrounded);
    58.     }
    59. }
    60.  
    upload_2018-6-27_15-24-22.png

    The thing is that each time i edit something down the animator window, this line stops wokring (it eval to True no matter what)

    Code (CSharp):
    1.   isGrounded = Physics2D.OverlapCircle(GroundCheck.position, groundCheckRadious, whatIsGround);

    the only thing that solves this weird behaviour is if i go to assets menu then reimport all... check the video below

    https://screencast-o-matic.com/watch/cF1tFgFrM4

    so.. i don't understand what is going on.

    weird bug, any thoughts?
     
  2. fonko

    fonko

    Joined:
    Mar 20, 2014
    Posts:
    23
    ok, so i found a workaround. It seems that this line of code is the problem, not sure why:

    GroundCheck = GameObject.Find("GroundCheck").transform;

    if i set this link from the editor itself, it works! (if i drag and drop this game object to the slot in playercontroller from the editor... it works ok !!!)

    seems to be something related to the lifecycle of the game object when pressing play...

    i hope this prevent somebody else to lose a couple of hours of life!