- Home /
Working Reference Still Throwing Null Reference Error
Hello everyone, I'm having a hard time figuring out what is happening to cause a null reference error in my scripts when in fact the reference is working.
My setup: I've got a loading screen that does an additive load to get the real game scene active, and when it detects a key object from the added scene, it turns off the loading screen ui and enables the in game ui (all of which uses NGUI).
The script also assigns a public script reference to a player movement script and a camera movement script (and this is where the problem occurs). In the scripts below, the player movement script throws a null reference error about "leftVirtualAnalogStick" at the line "if (leftVirtualAnalogStick.GetButtonPositionY() > deadzone)" (I've tried commenting it out but it just throws the error at the next if statement).
But, I have looked at the player movement script in the inspector and not only does it have the correct reference, but the input the reference gives it also works in game....so how is the reference null?
-Side Note: if I change the order of player and camera movement in the LoadMainGame script, both the player and camera movement scripts throw null reference errors.
using UnityEngine;
using System.Collections;
public class LoadMainGame : MonoBehaviour {
//this goes on parent container object for all loading screen objects
public GameObject worldRoot;//if found, it means the scene loaded,
public GameObject cameraLoadingScreen; //ngui, so can turn off this camera on load
public GameObject cameraInGameUI; //ngui,
public GameObject loadingScreenCamera;//the default camera that comes with a new scene,
public PlayerMovement playerMovement;
public CameraMovement cameraMovement;
void Awake()
{
}
void Start ()
{
Application.LoadLevelAdditive("test");
}
void Update ()
{
Debug.Log ("main menu script is updating");
worldRoot = GameObject.FindGameObjectWithTag("WorldRoot");
if (worldRoot != null) //does not count additive levels as true loads
{
cameraLoadingScreen.SetActive(false);
cameraInGameUI.SetActive (true);
loadingScreenCamera.SetActive (false);
//cameraMovement = GameObject.Find("0MainCamera").GetComponent<CameraMovement>();
//cameraMovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
playerMovement = GameObject.Find("PlayerPrefab").GetComponent<PlayerMovement>();
playerMovement.leftVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickLeft").GetComponent<VirtualAnalogStick>();
cameraMovement = GameObject.Find("0MainCamera").GetComponent<CameraMovement>();
cameraMovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
this.enabled = false;
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float playerForwardSpeed = 10f;
public float playerAcceleration = 1.1f;
public float playerDecceleration = 1.05f;
public float playerMaxForwardSpeed = 7f;
public float playerMaxBackwardSpeed = 4.5f;
public float playerCurrentSpeed;
public float rayCastDistance; //2.5 seems to work well, could put in timer if needed,
public float speed;
public bool speedClamp;
public Vector3 eulerAngleVelocity = new Vector3 (0f,5f,0f);
public Vector3 playerJumpHeight = new Vector3 (0f,5f,0f);
public Vector3 zDirection;
public VirtualAnalogStick leftVirtualAnalogStick; //move player
public float deadzone; //15 for now, area around 0 0 0 when button is considered no longer active (so say if player goes forward, he will stop before 0 0 0 is reached),
void Awake ()
{
//leftVirtualAnalogStick = null;
speedClamp = true;
//leftVirtualAnalogStick = GameObject.Find ("SpriteVirtualAnalogStickLeft").GetComponent<VirtualAnalogStick>();
}
void Start ()
{
//Debug.Log ("playermovement start has been called");
//leftVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickLeft").GetComponent<VirtualAnalogStick>();
}
void Update ()
{
KeyboardJump ();
}
void FixedUpdate ()
{
KeyboardMovePlayer ();
MovePlayer();
//Debug.Log ("Player velocity is " + rigidbody.velocity);
playerCurrentSpeed = Mathf.Abs(rigidbody.velocity.z);
//Debug.Log (rigidbody.velocity.magnitude);
//print ("Speed is" + playerCurrentSpeed);
//Debug.Log (speedClamp);
}
void MovePlayer ()
{
//Debug.Log (speedClamp);
//Quaternion positiveRotation = Quaternion.Euler(eulerAngleVelocity * Time.fixedDeltaTime);
//Quaternion negativeRotation = Quaternion.Euler(-eulerAngleVelocity * Time.fixedDeltaTime);
//setting forwards
if (leftVirtualAnalogStick.GetButtonPositionY() > deadzone)
{
zDirection = new Vector3 (rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z); // could move to awake so not creating each check?
//float zSpeed;
//float xSpeed;
zDirection += transform.forward * playerForwardSpeed * playerAcceleration * Time.fixedDeltaTime;
//zSpeed = zDirection.z;
//xSpeed = zDirection.x;
if (speedClamp == true)
{
if (zDirection.z > playerMaxForwardSpeed) {zDirection.z = playerMaxForwardSpeed;}
if (zDirection.x > playerMaxForwardSpeed) {zDirection.x = playerMaxForwardSpeed;}
if (zDirection.z < -playerMaxForwardSpeed) {zDirection.z = -playerMaxForwardSpeed;}
if (zDirection.x < -playerMaxForwardSpeed) {zDirection.x = -playerMaxForwardSpeed;}
}
rigidbody.velocity = zDirection;
//Debug.Log (zSpeed);
//Debug.Log (zDirection.z);
//Debug.Log (rigidbody.velocity.z);
//Debug.Log (rigidbody.velocity.magnitude);
}
//setting backwards
if (leftVirtualAnalogStick.GetButtonPositionY() < -deadzone)
{
Vector3 zDirection = new Vector3 (rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
//float zSpeed;
//float xSpeed;
zDirection += transform.forward * -playerForwardSpeed * playerAcceleration * Time.fixedDeltaTime;
//zSpeed = zDirection.z;
// = zDirection.x;
if (speedClamp == true)
{
if (zDirection.z > playerMaxBackwardSpeed) {zDirection.z = playerMaxBackwardSpeed;}
if (zDirection.x > playerMaxBackwardSpeed) {zDirection.x = playerMaxBackwardSpeed;}
if (zDirection.z < -playerMaxBackwardSpeed) {zDirection.z = -playerMaxBackwardSpeed;}
if (zDirection.x < -playerMaxBackwardSpeed) {zDirection.x = -playerMaxBackwardSpeed;}
}
rigidbody.velocity = zDirection;
//Debug.Log (zSpeed);
//Debug.Log (zDirection.z);
//Debug.Log (rigidbody.velocity.z);
//Debug.Log (rigidbody.velocity);
//Debug.Log (rigidbody.velocity.magnitude);
}
//setting rotation
if (leftVirtualAnalogStick.GetButtonPositionX() > deadzone)
{
rigidbody.angularVelocity = eulerAngleVelocity;
//rigidbody.MoveRotation(rigidbody.rotation * positiveRotation);
//Debug.Log (transform.forward.y);
}
if (leftVirtualAnalogStick.GetButtonPositionX() < -deadzone)
{
rigidbody.angularVelocity = -eulerAngleVelocity;
//rigidbody.MoveRotation(rigidbody.rotation * negativeRotation);
//Debug.Log (transform.forward.y);
}
}
Not exactly sure which script you are referencing to but: Load$$anonymous$$ainGame is on 1 GO Player$$anonymous$$ovement is on 1 GO Camera$$anonymous$$ovement is on 1 GO
tried moving the $$anonymous$$ove() method into Update ins$$anonymous$$d of FixedUpdate and the problem no longer occurs, but of course that leaves a physics operation in Update...so I don't think it is a good solution.
I meant the script which is giving the null reference error. You might have accidentally added an instance of that script to a GO that doesn't need to have it.
Ah, no - its definitely having an error with the script on the player, and there are no other instances of it.