Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question issue with OntriggerEnter

Discussion in 'Scripting' started by unity_PaUsMYSjmEWd7g, Apr 5, 2021.

  1. unity_PaUsMYSjmEWd7g

    unity_PaUsMYSjmEWd7g

    Joined:
    Apr 4, 2021
    Posts:
    2
    Hi to everyone, I'm experiencing an issue that I can't fix. I've checked tones of tutorials and they all say the same "check that both objects have collider and at least one of them a rigid body, and also the trigger object with isTrigered marked(I even tried rigid body on both with no gravity and iskinetic). Well, here is the thing I think they never collide for some reason, so I'll leave some screenshots for you to please help me to make it work.

    upload_2021-4-5_11-50-1.png
    This is the player that will collide with the object.
    upload_2021-4-5_11-51-21.png
    This is the capsule where I located the camera for the First person controller.
    upload_2021-4-5_11-52-1.png upload_2021-4-5_11-52-53.png

    upload_2021-4-5_11-55-14.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Well we know that collision didn't stop working, therefore something is wrong with the setup.

    For one, your OnTriggerEnter is inside the Update(). That alone prevents anything else outside of Update() from seeing it, so from Unity's standpoint your OnTriggerEnter doesn't even exist.

    Coding requires that 100% of everything is put in precisely the correct spot, not just sorta all in one big giant blob of things.
     
    jasonasaad2 likes this.
  3. unity_PaUsMYSjmEWd7g

    unity_PaUsMYSjmEWd7g

    Joined:
    Apr 4, 2021
    Posts:
    2
    Thanks for the positive answer, but I tried taking the update method off and it still doesn't work.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Again, we know that collision works. We also know that "it still doesn't work" isn't a useful reply.

    If you're new to collisions / triggers, work through some Youtube tutorials because there are very specific conditions that must be met, and those conditions are laid out very clearly in the documentation. There's no sense in me iterating them here for you.

    Isolate the triggering, verify it works with Debug.Log()

    Once it works, isolate whatever you are trying to do with it, make sure it works.

    In general I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
    hms0589 likes this.