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 dendens2 · Mar 16, 2013 at 04:48 PM · errorgameobjectruntime

Object reference not set to an instance of an object

Hi, so I have a script that makes it so that the hands attached to the gun model are not visible when you are not holding it. I have both variables assigned but when I run the game I get the runtime error shown above. Here is my script.

 var walkScript: PlayerMovementScript;
 var parentGun: GameObject;
 
 
 function Start () 
 {
     walkScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
 
 }
 
 function Update () 
 {
     if(walkScript.currentGun == parentGun)
     {
         gameObject.GetComponent(MeshRenderer).enabled = true;
     }
     if(walkScript.currentGun != parentGun)
     {
         gameObject.GetComponent(MeshRenderer).enabled = false;
     }
 }

Thanks!

Comment
Add comment · Show 6
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 nsxdavid · Mar 16, 2013 at 04:51 PM 0
Share

There are many places where something could be null (aka Object reference not set...) in that code. Which line is causing it?

avatar image robertbu · Mar 16, 2013 at 04:52 PM 0
Share

Listing the line the error occurred on (better yet commenting the line), is helpful in getting an accurate answer. At the end of Start(), put:

 if (walkScript == null)
     Debug.Log("Null walkScript");

If it is null, check to make sure your player game object is tagged with "Player", not just named "Player".

avatar image dendens2 · Mar 16, 2013 at 05:00 PM 0
Share

I get the error when the gameObject.GetComponent($$anonymous$$eshRenderer).enabled = false; runs

avatar image nsxdavid · Mar 16, 2013 at 05:05 PM 0
Share

If that's the line, then the object has no $$anonymous$$eshRenderer component and thus that returns null and thus .enabled on null goes boom.

avatar image dendens2 · Mar 16, 2013 at 05:15 PM 0
Share

Oh, wow. I didn't even notice it was Skinned$$anonymous$$eshRenderer. However, I still get an error from those same lines. If it helps, here is a screen. http://puu.sh/2iIVf

Show more comments

1 Reply

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

Answer by dendens2 · Mar 16, 2013 at 06:17 PM

Ok guys, figured it out. One of the hands was SkinnedMeshRenderer, the other was MeshRenderer, since it was not animated. Fixed it with this:

 var walkScript: PlayerMovementScript;
 var parentGun: GameObject;
 
 
 function Start () 
 {
     walkScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
 
 }
 
 function Update () 
 {
     if(walkScript.currentGun == parentGun)
     {
         if(gameObject.GetComponent(SkinnedMeshRenderer))
         {
             gameObject.GetComponent(SkinnedMeshRenderer).enabled = true;
         }
         else
         {
             gameObject.GetComponent(MeshRenderer).enabled = true;
 
         }
     }    
     if(walkScript.currentGun != parentGun)
     {
         if(gameObject.GetComponent(SkinnedMeshRenderer))
         {
             gameObject.GetComponent(SkinnedMeshRenderer).enabled = false;
         }
         else
         {
             gameObject.GetComponent(MeshRenderer).enabled = false;
 
         }
     }
 }

Thanks guys!

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

Follow this Question

Answers Answers and Comments

11 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

Related Questions

NaN exception when spawning soldiers 1 Answer

Weird GetComponent error 2 Answers

Why don't my gameobjects load in when I export my game to webgl?,Why don't any of my objects render when I export my game to webgl? 0 Answers

Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers

Compilor Error using Dictonary collection 1 Answer


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