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