- Home /
NullReference on Collision
I'm trying to make that a text object will only be visible once the player collides with the NPC, but the console displays this error everytime the collision occurs:
NullReferenceException: Object reference not set to an instance of an object NPCScript.OnTriggerEnter (UnityEngine.Collider talkTrigger) (at Assets/Scripts/NPCScript.js:14)
NullReferenceException: Object reference not set to an instance of an object NPCScript+$OnTriggerExit$1+$.MoveNext () (at Assets/Scripts/NPCScript.js:20)
This is my code:
var dialogue : UnityEngine.UI.Text;
var talkTrigger : GameObject;
function Start () {
talkTrigger = GameObject.Find("TalkTrigger");
dialogue = GetComponent(UI.Text);
dialogue.text = "Hello World";
dialogue.enabled = false;
}
function OnTriggerEnter (talkTrigger : Collider){
//if(Input.GetKeyDown("e"))
dialogue.enabled = true;
}
function OnTriggerExit (talkTrigger : Collider){
yield WaitForSeconds(5);
dialogue.enabled = true;
}
You should check the doc for GetComponent for javascript, you have a mistake here.
Do you imports properly : import UnityEngine.UI;
to get the component you can then do :
dialogue = GetComponent("Text");
I$$anonymous$$O dialogue is not well initialized in the Start() or you lose the reference for some reason when your trigger functions are called.
You should check that the object you are touching is the one you want, put a if inside the ontriggerenter, with a tag comparer, because maybe it's touching other thing
Yes, when i check the tag which is colliding it returns the FirstPersonController's tag, but it still works (doing what it's inside the OnTrigger when i collide with the GameObject which has the script attached to it).
How is the correct way to initialize dialogue
so I can manipulate it inside the OnTrigger functions? I tried to do what Nerevar said but it didn't solve the problem (but was very useful information, thanks).
I decided to write the script in C#, since it has way more help content on the web.
Here is the code, worked fine with no errors or warnings:
public class CScript : $$anonymous$$onoBehaviour {
private Text dialogue;
public string speech;
public void Start () {
dialogue = GameObject.Find("Dialogue").GetComponent<Text>();
dialogue.text = speech;
dialogue.enabled = false;
}
void OnTriggerStay(Collider c)
{
//Debug.Log(c);
if (Input.Get$$anonymous$$eyDown("e"))
dialogue.enabled = true;
}
IEnumerator OnTriggerExit(Collider c)
{
yield return new WaitForSeconds(3);
dialogue.enabled = false;
}
}
Don't know if I should add this to the main question, but if anyone is kind enough to explain me the differences I would aprreciate.
Answer by Jakeiiii · Jun 25, 2015 at 12:39 PM
I'm not fluent in JavaScript but are you meant to pass through 'talkTrigger' through your OnCollision parameters like that? As far as I'm concerned, it's being tiggered by other colliders, by any collider. You'd want to check that the Collider being passed through the parameter is your "Talk Trigger" object. Rather than writing OnTriggerEnter(talkTrigger : Collider), write OnTriggerEnter(col : Collider) and check that col.tag == "whatever talkTriggers tag is".
You were correct about the collider, but I still have problemas manipulating dialogue
in the OnTrigger functions. Thanks!