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): var manufatto: int=0; var vincita: int=7; function OnTriggerEnter(other: Collider){ if(other.GameObject.tag=="manufatto"){ manufatto+=1; Debug.log("Manufatto trovato! Sei a "+manufatto+" su "+vincita); Destroy(other.GameObject); } } function OnGUI(){ if(manufatto<vincita){ GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), ""+manufatto+" Manufatti"); } else{ GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "Trovati tutti i manufatti!!!"); } } 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
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.
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
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) Spoiler: Click for code Code (CSharp): using UnityEngine; public class OnTrigger : MonoBehaviour { int manufatto = 0; int vincita = 7; void OnTriggerEnter(Collider colliderInfo) { if(colliderInfo.gameObject.tag == "manufatto") { manufatto += 1; Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita); Destroy(colliderInfo.gameObject); } } } 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.
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). Spoiler: Click for code Code (CSharp): using UnityEngine; public class OnTrigger : MonoBehaviour { int manufatto = 0; int vincita = 7; GameObject someGameObject; void Start() { someGameObject = GameObject.Find("GameObjectDoesntExist"); Destroy(someGameObject); } void Update() { Debug.Log(someGameObject.name); //null reference happens here, doesnt do rest of code in this Update method, but does do code in ontrigger } void OnTriggerEnter(Collider colliderInfo) { if(colliderInfo.gameObject.tag == "manufatto") { manufatto += 1; Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita); Destroy(colliderInfo.gameObject); } } } This script will give a null reference, but it is inside the ontrigger so the ontrigger doesnt work (I have tested). Spoiler: Click for code Code (CSharp): using UnityEngine; public class OnTrigger : MonoBehaviour { int manufatto = 0; int vincita = 7; GameObject someGameObject; void Start() { someGameObject = GameObject.Find("GameObjectDoesntExist"); Destroy(someGameObject); } void OnTriggerEnter(Collider colliderInfo) { Debug.Log(someGameObject.name); //null reference happens here, doesnt do rest of code in this method if(colliderInfo.gameObject.tag == "manufatto") { manufatto += 1; Debug.Log("Manufatto trovato! Sei a " + manufatto + " su " + vincita); Destroy(colliderInfo.gameObject); } } }
other thing in your code you call "other.GameObject.tag" instead of "other.gameObject.tag", are you sure it's the right script ?
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!