• 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 /
  • Help Room /
avatar image
Question by Giullare · Jul 09, 2016 at 05:02 AM · c#errorinstanceinteractionnull reference exception

Not sure what this error is telling/asking me to do?

I watched a video on an interaction script (because I suck at scripting), and I did all according to plan. However, I get t$$anonymous$$s error message "NullReferenceException: Object reference not set to an instance of an object Interact.Update() (at Assets/Interact.cs:50)" and i have no idea what it is telling me to do in order to fix it.

My code: using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class Interact : MonoBehaviour
 {
     public string interactButton;
 
     public float InteractDistance = 3f;
     public LayerMask interactLayer; //filter
 
     public Image interactIcon; //Picture to show if you can interact or not
 
     public bool isInteracting;
 
     // Use t$$anonymous$$s for initialization
     void Start ()
     {
         //set interact icon to be invisible
         if(interactIcon != null)
         {
             interactIcon.enabled = false;
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit $$anonymous$$t;
 
         //Shoots a ray
         if(Physics.Raycast(ray, out $$anonymous$$t, InteractDistance, interactLayer))
         {
             //Checks if we are not interacting
             if (isInteracting == false)
             {
                 if(interactIcon != null)
                 {
                     interactIcon.enabled = true;
                 }
 
                 //If we press the interaction button
                 if (Input.GetButtonDown("Interact"))
                 {
                     //If it is a Door
                     if ($$anonymous$$t.collider.CompareTag("Door"))
                     {
                         //Open or Close it
                         $$anonymous$$t.collider.GetComponent<Door>().ChangeDoorState();
                     }
                 }
             }
         }
     }
 }


Line 50 = $$anonymous$$t.collider.GetComponent().ChangeDoorState();

I thought it was telling me to name the gameobject "Door" but i guess thats not the issue :( Thanks!

Comment

People who like this

0 Show 2
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 Giullare · Jul 09, 2016 at 05:04 AM 0
Share

Edit: The error occurs when I walk up to the door and press my Interact Button

avatar image Fappp · Jul 09, 2016 at 06:19 AM 0
Share

It basically means there is no door available to execute the action on.

(hit.collider.GetComponent() as Door).ChangeDoorState();

Try overloading the type. Also do a null check on the Door Script like this:

if ( !hit.collider.GetComponent() ){ return; }

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by jdean300 · Jul 09, 2016 at 06:18 AM

The object that the Ray cast $$anonymous$$t does not have a door component. Try using Debug.Log to print out the value of $$anonymous$$t.collider and $$anonymous$$t.collider.gameObject to make sure you are $$anonymous$$tting the object you expect, and make sure there is a Door component in that object.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help I can't find the error in my code! 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

NullReferenceException: Object reference not set to an instance of an object 3 Answers

How can i make a cannon ball go forward? 1 Answer

NPC moving on the same tile instead of walking around? 0 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