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 boddole · Mar 08, 2014 at 08:47 PM · c#programmingnullreferenceexception

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);
         }
     }
Comment
Add comment · Show 4
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 Jamora · Mar 08, 2014 at 09:09 PM 0
Share

Is the same script attached to other GOs as well?

avatar image boddole · Mar 08, 2014 at 09:20 PM 0
Share

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.

avatar image Jamora · Mar 08, 2014 at 10:04 PM 0
Share

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.

avatar image boddole · Mar 08, 2014 at 10:22 PM 0
Share

Ah, no - its definitely having an error with the script on the player, and there are no other instances of it.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Multiple Cars not working 1 Answer

NullReferenceException After A Few Hours? 1 Answer

C# Textfield Null Reference Exception 1 Answer

C# Null Reference Exception in Custom Function 1 Answer

C# Need Help Finding Where Null Reference Exception Is 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