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

Yauheni Kuzmich
Yauheni Kuzmich
1,529 Points

Why is the fly not getting destroyed?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyPickUp : MonoBehaviour {

    void OnTriggerEnter (Collider other) {

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

            Destroy (gameObject);

        }
    }
}

11 Answers

Oğuzhan Emre Özdoğan
Oğuzhan Emre Özdoğan
3,579 Points

the U has to be lower case so it should be like this:

public class FlyPickup : MonoBehaviour {

hope this helps!

The name of the class doesn't affect the code at all.

It doesn't at all. :(

B. Costas
B. Costas
4,182 Points

An answer for future generations :) , make sure that you don't make changes to the code while in "Play mode" since they will not be saved. The first time I created the FlyPickup script, I did it in play mode by accident. When I left Play mode, the FlyPickup script was no longer a component to the Fly object. All the code was right and the script existed in the script folder but it was not a component of the Fly object.

If you have this issue, go to the Fly Object and make sure the Fly Pickup (Script) is listed as a component in the Inspector. If not, delete the script in the script folder and Add the Component again.

Kevin Patel
Kevin Patel
3,014 Points

I had the same problem and it was fixed by re adding the script component to the fly inspector

Yauheni Kuzmich
Yauheni Kuzmich
1,529 Points

Thanks for the answer, but it did not help.

Yauheni Kuzmich
Yauheni Kuzmich
1,529 Points

So what's wrong then? Help me please!

Just posted an answer ;)

I can see no problem with your code, so I think you might have un-checked a parameter by accident. Make sure that the Player's tag is "Player" and look at the Fly inspector to make sure that the "Is Trigger" box is checked.

If it still doesn't work, restart Unity. If it doesn't work again, try copy and pasting my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class flyPickup : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            Destroy(gameObject);
        }
    }
}
Yauheni Kuzmich
Yauheni Kuzmich
1,529 Points

Thanks, but unfortunately it still does not work, the player tag and check mark is worth it, and your code is copied and pasted. I do not know what to do(

Strange. If your code is correct and the frog's collider interacts with the fly's it should work fine :/

Ask in Community instead, or request an answer from a more experienced member. Most people here are doing the course at the same time as you, including me, so we do not know every obscure detail about Unity. But the people in the Community have a lot more experience than us, they might be able to provide a better answer.

Sorry I can't be of more help.

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Yauheni Kuzmich - This is likely something wrong in the editor or another script, because the code looks correct to me.

Some things to check:

  • Is the script attached to the correct GameObject?
  • Does the filename match the class name? This is case sensitive.
  • Do you see any errors in the Console window?
  • Does the tag named in the script "Player" match the tag in the editor? This is also case sensitive.
Humayun Omar
Humayun Omar
1,093 Points

Solved: Change the name of the file of the FlyPickup to something else...I changed it to FlyPicking and it works now...Some error in unity saying that FlyPickup is already defined in global space.I also had no error in the code.It will work just change the file name

Harvey Specter
Harvey Specter
1,190 Points

I tried everything that is suggested here but still i have the same issue, the fly is not getting destroyed.

Fabian Fuentes
Fabian Fuentes
2,149 Points

On your code you are missing the space needed when using the parenthesis. you have this: OnTriggerEnter(Collider other) instead of this: OnTriggerEnter (Collider other)

also your if statement shows the same if(other...... if (other......

I hope this helps and sorry for not giving a more specific answer, I am just starting courses here at treehouse and I am not yet familiar with all the names for the different functions.

Fabian Fuentes
Fabian Fuentes
2,149 Points

Sorry but I think the code still works even when the spacing is off I should have tried it before answering but I did have the same problem, the problem was that the flyPickup script wasn't attached to the component on the fly object so I deleted the component not the script and attached it again, once I did this the game worked perfect.

Yes, it definitely does, I actually prefer it without the space. Most of coding is case sensitive tho, so even if you are not sure it is worth giving it a shot.