vendredi 25 mai 2018

Unity C#: If-Statement hadn't been obeserved

in my Unity project I have a script attached to a couple of prefabs. Every few seconds a random prefab is spawned. This is a part of my attached script:

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.CompareTag("ground"))
        {
            if (transform.gameObject.name == "FallingKeule(Clone)") 
            {
                Destroy(transform.gameObject);
            }
            if (transform.gameObject.name == "FallingHeart(Clone)")
            {
                Destroy(transform.gameObject);
            }
            if (transform.gameObject.name == "FallingCup(Clone)")
            {
                Destroy(transform.gameObject);
            }
            else
            {
                print("You lost a life!");
                Player.GetComponent<Colliding>().LostLife();
                Destroy(transform.gameObject);
            }

        }
    }

If a GameObject is spawned by random and it hit's the ground, and it is a "FallingKeule(Clone)" --> "(Clone)" because the prefab is cloned by it's initalisation the Code from

if (transform.gameObject.name == "FallingKeule(Clone)") 

isn't be done! The else code is been done:

else
                {
                    print("You lost a life!");
                    Player.GetComponent<Colliding>().LostLife();
                    Destroy(transform.gameObject);
                }

I have no clue what I have done wrong but I'm really thanksfull for all answers.

Jul

Aucun commentaire:

Enregistrer un commentaire