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 trialYauheni Kuzmich
1,529 PointsWhy 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
3,579 Pointsthe U has to be lower case so it should be like this:
public class FlyPickup : MonoBehaviour {
hope this helps!
B. Costas
4,182 PointsAn 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
3,014 PointsI had the same problem and it was fixed by re adding the script component to the fly inspector
Yauheni Kuzmich
1,529 PointsThanks for the answer, but it did not help.
Yauheni Kuzmich
1,529 PointsSo what's wrong then? Help me please!
Samuel Savaria
4,809 PointsJust posted an answer ;)
Samuel Savaria
4,809 PointsI 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
1,529 PointsThanks, 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(
Samuel Savaria
4,809 PointsStrange. 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
Treehouse TeacherYauheni 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
1,093 PointsSolved: 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
1,190 PointsI tried everything that is suggested here but still i have the same issue, the fly is not getting destroyed.
Fabian Fuentes
2,149 PointsOn 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
2,149 PointsSorry 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.
Samuel Savaria
4,809 PointsYes, 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.
Samuel Savaria
4,809 PointsSamuel Savaria
4,809 PointsThe name of the class doesn't affect the code at all.
joannadouba
558 Pointsjoannadouba
558 PointsIt doesn't at all. :(