Search Unity

OnTriggerEnter doesn't work?

Discussion in 'Physics' started by help_96, Jun 15, 2015.

  1. help_96

    help_96

    Joined:
    Jun 10, 2015
    Posts:
    18
    hi, my problem is that i want to destroy an object when i touch it. I follow a lot of tutoril step by step and some threads of this forum too but i can't do what i want... the result is that i switch throught the object. I put on cube the trigger collider and on the FirstPersonController the script with function OnTriggerEnter. This is the code:
    Code (JavaScript):
    1.  
    2. var manufatto: int=0;
    3. var vincita: int=7;
    4.  
    5. function OnTriggerEnter(other: Collider){
    6. if(other.GameObject.tag=="manufatto"){
    7. manufatto+=1;
    8. Debug.log("Manufatto trovato! Sei a "+manufatto+" su "+vincita);
    9. Destroy(other.GameObject);
    10. }
    11. }
    12.  
    13. function OnGUI(){
    14. if(manufatto<vincita){
    15. GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), ""+manufatto+" Manufatti");
    16. }
    17. else{
    18. GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "Trovati tutti i manufatti!!!");
    19. }
    20. }
    21.  
    Some strings are italian but the function are the same
    I attach an image with the ispector of FirstPersonController and the cube(the object which i wanto to destroy). I also give the right tag to it
     

    Attached Files:

  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    at least one of the gameobjects needs to be a rigidbody.
    Put it on your cube since having a rigidbody and a charactercontroller is advised against.
     
  3. help_96

    help_96

    Joined:
    Jun 10, 2015
    Posts:
    18
    If i put on the cube a rigidbody nothing change...
    The CharacterController is on the FirstPersonController by default whitout its i can't move myself. I don't understand what you mean sorry
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I did a test, and even when both objects dont have a rigidbody, using this script works... (I guess character controller counts as a rigidbody)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OnTrigger : MonoBehaviour
    4. {
    5.     int manufatto = 0;
    6.     int vincita = 7;
    7.  
    8.     void OnTriggerEnter(Collider colliderInfo)
    9.     {
    10.         if(colliderInfo.gameObject.tag == "manufatto")
    11.         {
    12.             manufatto += 1;
    13.             Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita);
    14.             Destroy(colliderInfo.gameObject);
    15.         }
    16.     }
    17. }

    If you put that c# code on your character, does it work? (make sure you make a c# file, not a javascript one).
    I dont know javascript, but you have "other.GameObject" and not "other.gameObject". In c#, this wouldn't even compile, but maybe in js it is correct, or it is meaning something else? What I am thinking you may have done is not copied and pasted your code, but typed it here? Because you also have "Debug.log" and not "Debug.Log".
    In your js file, before you do the if statement, do a debug.log to check if the trigger is at least running.

    I'm pretty sure I read somewhere that having a rigidbody(probably non kinematic) on a character controller is advised against. So I told you to put it on the cube just in case. You don't have to do this as with my test, it seems the Character Controller acts as a rigidbody.
     
    Last edited: Jun 16, 2015
  5. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    you need to resolve the NullReferenceExecption that you have in the console first
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I guess maybe that exception might be causing the trigger not to work if he had more code in the trigger that he isn't telling us, but if this is all the code there is, then even if there is an exception somewhere else in his code, from my quick test, it shouldn't effect the trigger.

    Example..
    This script will give a null reference, but it is outside the ontrigger so the ontrigger still works (I have tested).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OnTrigger : MonoBehaviour
    4. {
    5.     int manufatto = 0;
    6.     int vincita = 7;
    7.     GameObject someGameObject;
    8.  
    9.     void Start()
    10.     {
    11.         someGameObject = GameObject.Find("GameObjectDoesntExist");
    12.         Destroy(someGameObject);
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Debug.Log(someGameObject.name); //null reference happens here, doesnt do rest of code in this Update method, but does do code in ontrigger
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider colliderInfo)
    21.     {
    22.         if(colliderInfo.gameObject.tag == "manufatto")
    23.         {
    24.             manufatto += 1;
    25.             Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita);
    26.             Destroy(colliderInfo.gameObject);
    27.         }
    28.     }
    29. }

    This script will give a null reference, but it is inside the ontrigger so the ontrigger doesnt work (I have tested).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OnTrigger : MonoBehaviour
    4. {
    5.     int manufatto = 0;
    6.     int vincita = 7;
    7.     GameObject someGameObject;
    8.  
    9.     void Start()
    10.     {
    11.         someGameObject = GameObject.Find("GameObjectDoesntExist");
    12.         Destroy(someGameObject);
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider colliderInfo)
    16.     {
    17.         Debug.Log(someGameObject.name); //null reference happens here, doesnt do rest of code in this method
    18.         if(colliderInfo.gameObject.tag == "manufatto")
    19.         {
    20.             manufatto += 1;
    21.             Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita);
    22.             Destroy(colliderInfo.gameObject);
    23.         }
    24.     }
    25. }
     
  7. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    other thing in your code you call "other.GameObject.tag" instead of "other.gameObject.tag", are you sure it's the right script ?
     
    hopetolive likes this.
  8. help_96

    help_96

    Joined:
    Jun 10, 2015
    Posts:
    18
    Ohhh thank you so much! I m very stupid the error are the string gameObject and Debug.Log that i wrote wrong i apologise... I don't thought that the unity compiler doesn't find the syntax errors, in all other compiler that i use they are found D:
    thank you again!
     
  9. kkdevakyea

    kkdevakyea

    Joined:
    Sep 6, 2020
    Posts:
    2
    THANK U