Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I am making a code to switch between two modules of birds there isnt any errors but it dosent work.

Discussion in 'Getting Started' started by yassdev69, May 26, 2021.

  1. yassdev69

    yassdev69

    Joined:
    Feb 25, 2021
    Posts:
    1
    public static bool Grounded = true;

    public GameObject Bird1;
    public GameObject Bird2;

    // Start is called before the first frame update
    void Start()
    {
    Bird1.SetActive(false);
    Bird2.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    if(Grounded == true)
    {
    Bird1.SetActive(false);
    }

    if(Grounded == true);
    {
    Bird2.SetActive(true);
    }

    if(Grounded == false)
    {
    Bird1.SetActive(true);
    }

    if(Grounded == false);
    {
    Bird2.SetActive(false);
    }

    CheckIfGrounded();

    }

    private void OnCollisionEnter(Collision col)
    {
    CheckIfGrounded();

    }

    private void CheckIfGrounded()
    {
    RaycastHit[] hits;

    //We raycast down 1 pixel from this position to check for a collider
    Vector3 positionToCheck = transform.position;
    hits = Physics.RaycastAll (positionToCheck, new Vector3 (0, -1), 0.01f);

    //if a collider was hit, we are grounded
    if (hits.Length > 0) {
    Grounded = true;
    }
    }
    private void OnCollisionExit(Collision col)
    {
    Grounded = false;
    }
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome to the forums! You should definitely use code tags when sharing code, as it formats your code and makes it easier to read.

    Why is your Grounded property static? And are you sure your raycast is actually hitting the objects? You can use Debug.Log inside that check (~line 55) or set breakpoints to inspect your properties and find out. Maybe your birbs don't have colliders on them or something. It's really tough to say for sure by just looking at part of a script and no details beyond "doesn't work."

    If you dig a little deeper, and try explaining it in a way that will make sense to people who have no previous knowledge of your project (i.e., all of us), we can probably tell you something a bit more specific.