Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vcoelho · Jun 25, 2015 at 09:21 AM · javascriptcollision

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;
 }
 

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Jun 25, 2015 at 09:25 AM 0
Share

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.

avatar image Kirbyrawr · Jun 25, 2015 at 11:38 AM 0
Share

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

avatar image vcoelho · Jun 26, 2015 at 12:23 PM 0
Share

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).

avatar image vcoelho · Jun 26, 2015 at 01:30 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vcoelho · Jun 26, 2015 at 12:24 PM 0
Share

You were correct about the collider, but I still have problemas manipulating dialogue in the OnTrigger functions. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Collision Detection in Capsule Collider 0 Answers

OnTriggerEnter not working for NavMesh 1 Answer

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

OnCollisionStay for seconds then destroy 0 Answers

Destroy a GameObject 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges