Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Pickups Script the Fly Pickup

Marc Martinez
Marc Martinez
12,909 Points

My fly won't disappear

I can't get my fly to disappear on my script. I'm not sure what is wrong with my code

using UnityEngine;
using System.Collections;

public class FlyPickup : MonoBehaviour {

    void onTriggerEnter(Collider other) {
        // if the "Collider" other is tagged with "Player"...
        if (other.CompareTag ("Player")) {

            Destroy (gameObject);
        }
    }
}

5 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Marc,

My guess is that your OnTriggerEnter() method needs to start with a capital letter. C# is a case sensitive language, so onTriggerEnter() is different than OnTriggerEnter().

I hope that helps!

Marc Martinez
Marc Martinez
12,909 Points

Yes!! It worked! Thank you so much I never would have guessed it.

Also,

Make sure that the "FlyPickup" script has been added to to "Fly" in the Hierarchy. Nick walks you through this right in the beginning of the video.

My "FlyPickup" script wasnt added to "Fly" and the fly did not disappear when i moved the frog into it.

This may be the source of the problem for those of you who have the code typed out correctly but it's still not working.

Thank you very much!!!! Even I am not the one asking I was struggling with this too

Alejandro Chiari You're welcome! Dont forget to up vote my answer if it was helpful.

Happy coding.

Matt Summers
Matt Summers
3,036 Points

Thanks Amandeep! Not sure what happened to my script when I created it but it somehow wasn't linked to the fly. Adding it fixed my problem.

jason chan
jason chan
31,009 Points
using UnityEngine;
using System.Collections;

public class FlyPickUp : MonoBehaviour {

    void OnTriggerEnter(Collider other) {
        // if collider other is tagged with player ... excecute code
        if (other.CompareTag ("Player")){
            Destroy (gameObject);
        }
    }
}
Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

Did you add the tag "Player" or the correct tag to the game object?

Or Tray to change onTriggerEnter() to OnTriggerEnter()

Pesky details, thanks for the tip!!! Completely missed the capital O.