Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by SweetPotatoes · Nov 02, 2014 at 05:01 PM · c#instanceobject reference

NullReferenceException Issues

Hi everyone. I have what must be a simple question, but it has been keeping me stuck for a while now.

I have be writing a script that can access a method from other scripts which are attached to the character's limbs. I have been working on the elbow as the starting point. However I have hit a brick wall, when I play the game and press thew W key to move the elbow this error appears in the console. "NullReferenceException: Object reference not set to an instance of an object MouseOnLimbCont.FixedUpdate()(At Assets/Code/Scripts/MouseOnLimbCont.cs20)"

This is the MouseOnLimbCont.cs

 using UnityEngine;
 using System.Collections;
 
 public class MouseOnLimbCont : MonoBehaviour {
 
     private GameObject rightElbow;
 
     void Start(){
 
         Screen.lockCursor = true;
         rightElbow = GameObject.Find("charTPose/charNode01/globalMove01/joints01/bn_r_arm001/bn_r_elbow001");
 
     }
 
     void FixedUpdate(){
 
         //Controls the Right Elbow of CharTPose
         if (Input.GetKey(KeyCode.W)) 
         {
             rightElbow.GetComponent<wKeyMoveLimb>().RightElbow();
         }
     } 
 }

This is the wKeyMoveLimb.cs

 using UnityEngine;
 using System.Collections;
 
 public class wKeyMoveLimb : MonoBehaviour {
 
     public float horizontalSpeed = 5.5f;
     public float verticalSpeed = 5.5f;
 
     public void RightElbow()
     {
         //Controls the Right Elbow of CharTPose
 
         float h = horizontalSpeed * Input.GetAxis ("Mouse X");
         float y = verticalSpeed * Input.GetAxis ("Mouse Y");
         rigidbody.AddForce(h,0, y);
     }
 }

Thank you in advance.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jhocking · Nov 02, 2014 at 05:06 PM

Well the error message is quite clear about the problem: the object reference on line 20 is not set. I see two object references on that line, either of which could be the problem: that code assumes 'rightElbow' is set and that it has a component 'wKeyMoveLimb'

Of course that brings up the question of why that object reference is empty. Either 'rightElbow' isn't being correctly set by the Find() command earlier, or 'wKeyMoveLimb' isn't attached to the object.

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
avatar image
0

Answer by robertbu · Nov 02, 2014 at 05:07 PM

This Find() appears to be failing:

  rightElbow = GameObject.Find("charTPose/charNode01/globalMove01/joints01/bn_r_arm001/bn_r_elbow001");

If this is not the full name of the elbow game object (i.e. it is a path), then you are looking for transform.Find() not GameObject.Find(). You will need to start the path with the first child of the transform you use in the Transform.Find(). To debug, pull the path apart and gradually extend it, checking with a Debug.Log() as each element of the path is added. If you get a null, then you know you've messed up the path.

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
avatar image
0

Answer by SweetPotatoes · Nov 15, 2014 at 03:10 AM

Thanks to the both of you for your suggestions they helped me out a ton. I did some more research on the matter and I found out that I need to make the GameObject public so I could then assign a joint from the character rig. I also made the scripts that are attached to each limb variables.

     public GameObject leftFoot;
     public GameObject rightFoot;
     public GameObject leftArm;
     public GameObject rightArm;
     private wKeyMoveLimb wkeymovelimb;
     private sKeyMoveLimb skeymovelimb;
     private dKeyMoveLimb dkeymovelimb;
     private aKeyMoveLimb akeymovelimb;
 

Then I used the GetComponet on each public GameObject and made it into the equation of the keymovelimb variables.

 void Awake(){
         
         wkeymovelimb = leftFoot.GetComponent<wKeyMoveLimb>();
         skeymovelimb = rightFoot.GetComponent<sKeyMoveLimb>();
         dkeymovelimb = leftArm.GetComponent<dKeyMoveLimb>();
         akeymovelimb = rightArm.GetComponent<aKeyMoveLimb>();

Finally to map each limb to the WASD I made an if Statement with a Input.GetKey(KeyCode.key) for when the player holds the selected key the line of code below it runs.

 void FixedUpdate(){
         
         //Controls the Left Foot of CharTPose
 
         if (Input.GetKey(KeyCode.W)) 
         {
             wkeymovelimb.LeftFoot();
         }

I know my terminology may be slightly off but I hope this helps someone else.

Also this is one of the scripts that the private keymovelimb will be accessing.

 using UnityEngine;
 using System.Collections;
 
 public class wKeyMoveLimb : MonoBehaviour {
 
     public float horizontalSpeed = 5.5f;
     public float verticalSpeed = 5.5f;
 
     public void LeftFoot()
     {
         //Controls the Left Foot of CharTPose
 
         float h = horizontalSpeed * Input.GetAxis ("Mouse X");
         float y = verticalSpeed * Input.GetAxis ("Mouse Y");
         rigidbody.AddForce(h,0, y);
     }
 } 

 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object 1 Answer

an object reference is required to access non-static member 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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