Why create new script?

Before this lecture, I made a key for the door which is linked to a public bool “hasKey” which the Level Exit script reads to either display “Key Needed” or advance to the next level. I did this in the player script as follows:

void OnTiggerEnter2D(Collider2D other)
if(other.tag == "Key")
        {
            hasKey = true;
            Destroy(other.gameObject);
        }

Why can’t I simply add another bit to the same method saying:

 if(other.tag == "Coin")
        {
            Destroy(other.gameObject);
        }

This would prevent the error Rick had where he forgot to add the script to the game object as the player is handling interacting with the rest of the world.

Annnnd the next video explains it - audio sources!

1 Like

Does that mean your question is answered? :slight_smile:


See also:

Yup! By applying the script to the asset being interacted with cleans up the scripts and makes it easier to make additional changes.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms