Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by miramaslow · Nov 13, 2015 at 09:50 PM · instance

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

Hi I'm kinda new to Unity and I have followed some tutorial on the Internet and I just want to be able to pick up the cutlery available in my game. This maybe the most mainstream question but I really need some help here because I don't know where or what to fix

Here's my script named InteractScript

 using UnityEngine;
 using System.Collections;
 
 public class InteractScript : MonoBehaviour {
     public float range;
     public float interactDistance = 5f;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.Mouse0))
         {
             Ray ray = new Ray(transform.position,transform.forward);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit,interactDistance))
             {
                 if(hit.collider.CompareTag("Door"))
                 {
                     hit.collider.transform.parent.GetComponent<doorscript>().ChangeDoorState();
                 }
                 if(hit.collider.CompareTag("BigDoor"))
                 {
                     hit.collider.transform.GetComponent<BigDoor>().ChangeDoorState();
                 }
                 if(hit.collider.CompareTag("Oven"))
                 {
                     hit.collider.transform.GetComponent<Oven>().ChangeDoorState();
                 }
                 if(hit.collider.CompareTag("SlidingDoor"))
                 {
                     hit.collider.GetComponent<Animation>().Play();
                 }
                 if(hit.collider.CompareTag("Cutlery"))
                 {
                     hit.collider.GetComponent<Cutlery>().ChangeCutleryState();
                 }
             }
         }
     }
 }

When I try to click on my cutlery, this shows up

NullReferenceException: Object reference not set to an instance of an object InteractScript.Update () (at Assets/Script/InteractScript.cs:39)

It says on line 39 but I don't see it

Any help will be appreciated

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by joemane22 · Nov 14, 2015 at 07:20 AM

You need to do

  hit.collider.gameObject.GetComponent<Cutlery>().ChangeCutleryState();

because you are trying to get the collider gameobject, instead of the gameobject the collider is attached to, component. Also, make sure all the objects have the right component attached.

Also, I'm not 100% sure that you have to do this just because I have always since I can remember, went to the gameobject every time I need to get a component, but I am pretty sure this is the case.

Comment
Add comment · Show 2 · 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 miramaslow · Nov 14, 2015 at 02:16 PM 0
Share

Oh thanks a lot mate, i've been so stupid not realized it haha sorry. Thanks again

avatar image Negative_Toast · Nov 28, 2020 at 06:41 PM 0
Share

Can you help me I have been geting the same error but I want to make my player move. https://answers.unity.com/questions/1792801/what-do-i-do-to-fix-this-error-i-get-it-twice-i-wa.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException 5 Answers

Object reference not set to an instance of an object 0 Answers

Damage function affecting all instances of object 1 Answer

Can we anyhow store a prefab in a list when we collide into the instance of that prefab casted as gameObject? 1 Answer

Saving a Player Pref Exclusive to that instance of a script 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