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 MRollins · Apr 22, 2013 at 01:12 AM · nullreferenceexception

Help with NullReferenceException

I am building off of the 2D Platformer tutorial and I have converted the sctrip for PlatformerController from javascript to C# but I'm am getting the following error at Runtime. The scipt is selected in the editor and when it runs with Javascript script it works fine. HJow can set the Object Reference to an object? Wouldn't assigning the script to the player in the editor assign it to the player and wouldn;t that be the object?

Error: NullReferenceException: Object reference not set to an instance of an object PlatformerController.Awake () (at Assets/Scripts/2D/Character Controls/PlatformerController.cs:125)

Note: Line 125 stis Line 11 below.

Thanks!

 public PlatformerControllerJumping jump;
 
 CharacterController controller;
 
 
 // This is used to keep track of special effects in UpdateEffects ();
 private bool areEmittersOn= false;
 
 
 public void  Awake (){
     movement.direction = transform.TransformDirection (Vector3.forward);
     controller = GetComponent<CharacterController>();
     Spawn ();
 }
 
 public void  Spawn (){
     // reset the character's speed
     movement.verticalSpeed = 0.0f;
     movement.speed = 0.0f;
     
     // reset the character's position to the spawnPoint
     transform.position = spawnPoint.position;
     
 }


Here is the entire script:

 // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
 // Do test the code! You usually need to change a few small bits.
 
 using UnityEngine;
 using System.Collections;
 
 public class PlatformerController : MonoBehaviour {
 
 // Does this script currently respond to Input?
     public bool canControl= true;
 
 // The character will spawn at spawnPoint's position when needed.  This could be changed via a script at runtime to implement, e.g. waypoints/savepoints.
 public Transform spawnPoint;
 
     void Start () {
     
     }
     
     public class PlatformerControllerMovement {
     // The speed when walking 
     public float walkSpeed= 10.0f;
     // when pressing "Fire1" button (control) we start running
     public float runSpeed= 10.0f;
 
     public float inAirControlAcceleration= 1.0f;
 
     // The gravity for the character
     public float gravity= 60.0f;
     public float maxFallSpeed= 20.0f;
 
     // How fast does the character change speeds?  Higher is faster.
     public float speedSmoothing= 5.0f;
 
     // This controls how fast the graphics of the character "turn around" when the player turns around using the controls.
     public float rotationSmoothing= 10.0f;
 
     // The current move direction in x-y.  This will always been (1,0,0) or (-1,0,0)
     // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view.  Very handy for organization!
     //@System.NonSerialized
     public Vector3 direction= Vector3.zero;
 
     // The current vertical speed
     //@System.NonSerialized
     public float verticalSpeed= 0.0f;
 
     // The current movement speed.  This gets smoothed by speedSmoothing.
     //@System.NonSerialized
     public float speed= 0.0f;
 
     // Is the user pressing the left or right movement keys?
     //@System.NonSerialized
     public bool isMoving= false;
 
     // The last collision flags returned from controller.Move
     //@System.NonSerialized
     public CollisionFlags collisionFlags; 
 
     // We will keep track of an approximation of the character's current velocity, so that we return it from GetVelocity () for our camera to use for prediction.
     //@System.NonSerialized
     public Vector3 velocity;
     
     // This keeps track of our current velocity while we're not grounded?
     //@System.NonSerialized
     public Vector3 inAirVelocity= Vector3.zero;
 
     // This will keep track of how long we have we been in the air (not grounded)
     //@System.NonSerialized
     public float hangTime= 0.0f;
 }
 
 public PlatformerControllerMovement movement;
 
 // We will contain all the jumping related variables in one helper class for clarity.
 public class PlatformerControllerJumping {
     // Can the character jump?
     public bool enabled= true;
 
     // How high do we jump when pressing jump and letting go immediately
     public float height= 1.0f;
     // We add extraHeight units (meters) on top when holding the button down longer while jumping
     public float extraHeight= 4.1f;
     
     // This prevents inordinarily too quick jumping
     // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view.  Very handy for organization!
     //@System.NonSerialized
     public float repeatTime= 0.05f;
 
     //@System.NonSerialized
     public float timeout= 0.15f;
 
     // Are we jumping? (Initiated with jump button and not grounded yet)
     //@System.NonSerialized
     public bool jumping= false;
     
     //@System.NonSerialized
     public bool reachedApex= false;
   
     // Last time the jump button was clicked down
     //@System.NonSerialized
     public float lastButtonTime= -10.0f;
     
     // Last time we performed a jump
     //@System.NonSerialized
     public float lastTime= -1.0f;
 
     // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
     //@System.NonSerialized
     public float lastStartHeight= 0.0f;
 }
 
 public PlatformerControllerJumping jump;
 
  CharacterController controller;
 
 // Moving platform support.
 private Transform activePlatform;
 private Vector3 activeLocalPlatformPoint;
 private Vector3 activeGlobalPlatformPoint;
 private Vector3 lastPlatformVelocity;
 
 // This is used to keep track of special effects in UpdateEffects ();
 private bool areEmittersOn= false;
 
 public void  Awake (){
     movement.direction = transform.TransformDirection (Vector3.forward);
     controller = GetComponent<CharacterController>();
     Spawn ();
 }
 
 public void  Spawn (){
     // reset the character's speed
     movement.verticalSpeed = 0.0f;
     movement.speed = 0.0f;
     
     // reset the character's position to the spawnPoint
     transform.position = spawnPoint.position;
     
 }
     
 void  OnDeath (){
     Spawn ();
 }
 
 public void  UpdateSmoothedMovementDirection (){    
     float h= Input.GetAxisRaw ("Horizontal");
     
     if (!canControl)
         h = 0.0f;
     
     movement.isMoving = Mathf.Abs (h) > 0.1f;
         
     if (movement.isMoving)
         movement.direction = new Vector3(h, 0, 0);
             
     // Grounded controls
     if (controller.isGrounded) {
         // Smooth the speed based on the current target direction
          float curSmooth= movement.speedSmoothing * Time.deltaTime;
         
         // Choose target speed
          float targetSpeed= Mathf.Min (Mathf.Abs(h), 1.0f);
     
          //Pick speed modifier
         if (Input.GetButton ("Fire2") && canControl)
             targetSpeed *= movement.runSpeed;
         else
             targetSpeed *= movement.runSpeed;
         
         movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);
         
         movement.hangTime = 0.0f;
     }
     else {
         // In air controls
         movement.hangTime += Time.deltaTime;
         if (movement.isMoving)
             movement.inAirVelocity += new Vector3(Mathf.Sign(h), 0, 0) * Time.deltaTime * movement.inAirControlAcceleration;
     }
 }
 
 void  FixedUpdate (){
     // Make sure we are absolutely always in the 2D plane. Doesn't do anything right now.
     Vector3 temp = transform.position; 
     temp.z = 0;
     //transform.position.z = temp;
 
 }
 
 public void  ApplyJumping (){
     // Prevent jumping too fast after each other
     if (jump.lastTime + jump.repeatTime > Time.time)
         return;
 
     if (controller.isGrounded) {
         // Jump
         // - Only when pressing the button down
         // - With a timeout so you can press the button slightly before landing        
         if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout) {
             movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
             movement.inAirVelocity = lastPlatformVelocity;
             SendMessage ("DidJump", SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 
 public void  ApplyGravity (){
     // Apply gravity
     bool jumpButton= Input.GetButton ("Jump");
     
     if (!canControl)
         jumpButton = false;
     
     // When we reach the apex of the jump we send out a message
     if (jump.jumping && !jump.reachedApex && movement.verticalSpeed <= 0.0f) {
         jump.reachedApex = true;
         SendMessage ("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
     }
     
     // * When jumping up we don't apply gravity for some time when the user is holding the jump button
     //   This gives more control over jump height by pressing the button longer
     bool extraPowerJump=  jump.jumping && movement.verticalSpeed > 0.0f && jumpButton && transform.position.y < jump.lastStartHeight + jump.extraHeight && !IsTouchingCeiling ();
     
     if (extraPowerJump)
         return;
     else if (controller.isGrounded)
         movement.verticalSpeed = -movement.gravity * Time.deltaTime;
     else
         movement.verticalSpeed -= movement.gravity * Time.deltaTime;
         
     // Make sure we don't fall any faster than maxFallSpeed.  This gives our character a terminal velocity.
     movement.verticalSpeed = Mathf.Max (movement.verticalSpeed, -movement.maxFallSpeed);
 }
 
 public float  CalculateJumpVerticalSpeed ( float targetJumpHeight  ){
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
 }
 
 void  DidJump (){
     jump.jumping = true;
     jump.reachedApex = false;
     jump.lastTime = Time.time;
     jump.lastStartHeight = transform.position.y;
     jump.lastButtonTime = -10;
 }
 
 //void  UpdateEffects (){
 //    wereEmittersOn = areEmittersOn;
 //    areEmittersOn = jump.jumping && movement.verticalSpeed > 0.0f;
     
     // By comparing the previous value of areEmittersOn to the new one, we will only update the particle emitters when needed
 //    if (wereEmittersOn != areEmittersOn) {
 //        for ( emitter in GetComponentsInChildren (ParticleEmitter)) {
 //            emitter.emit = areEmittersOn;
 //        }
 //        }
 //    }
 
 
 void  Update (){
     if (Input.GetButtonDown ("Jump") && canControl) {
         jump.lastButtonTime = Time.time;
     }
 
     UpdateSmoothedMovementDirection(); 
     
     
     // Apply gravity
     // - extra power jump modifies gravity
     ApplyGravity ();
 
     // Apply jumping logic
     ApplyJumping ();
     
 
     
     // Moving platform support
     if (activePlatform != null) {
         Vector3 newGlobalPlatformPoint= activePlatform.TransformPoint(activeLocalPlatformPoint);
         Vector3 moveDistance= (newGlobalPlatformPoint - activeGlobalPlatformPoint);
         transform.position = transform.position + moveDistance;
         lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
     } else {
         lastPlatformVelocity = Vector3.zero;    
     }
     
     activePlatform = null;
 
 
     
     // Save lastPosition for velocity calculation.
     Vector3 lastPosition = transform.position;
     
     // Calculate actual motion
     Vector3 currentMovementOffset= movement.direction * movement.speed + new Vector3(0, movement.verticalSpeed, 0) + movement.inAirVelocity;
     
     // We always want the movement to be framerate independent.  Multiplying by Time.deltaTime does this.
     currentMovementOffset *= Time.deltaTime;
     
        // Move our character!
     movement.collisionFlags = controller.Move (currentMovementOffset);
     
     // Calculate the velocity based on the current and previous position.  
     // This means our velocity will only be the amount the character actually moved as a result of collisions.
     movement.velocity = (transform.position - lastPosition) / Time.deltaTime;
     
     // Moving platforms support
     if (activePlatform != null) {
         activeGlobalPlatformPoint = transform.position;
         activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
     }
     
     // Set rotation to the move direction    
     if (movement.direction.sqrMagnitude > 0.01f)
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);
     
     // We are in jump mode but just became grounded
     if (controller.isGrounded) {
         movement.inAirVelocity = Vector3.zero;
         if (jump.jumping) {
             jump.jumping = false;
             SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver);
 
             Vector3 jumpMoveDirection= movement.direction * movement.speed + movement.inAirVelocity;
             if (jumpMoveDirection.sqrMagnitude > 0.01f)
                 movement.direction = jumpMoveDirection.normalized;
         }
     }    
 
     // Update special effects like rocket pack particle effects
 //    UpdateEffects ();
 
     }
 
 
 
 void  OnControllerColliderHit ( ControllerColliderHit hit  ){
     if (hit.moveDirection.y > 0.01f) 
         return;
     
     // Make sure we are really standing on a straight platform
     // Not on the underside of one and not falling down from it either!
     if (hit.moveDirection.y < -0.9f && hit.normal.y > 0.9f) {
         activePlatform = hit.collider.transform;    
     }
 }
 
 // Various helper functions below:
 public float  GetSpeed (){
     return movement.speed;
 }
 
 public Vector3  GetVelocity (){
     return movement.velocity;
 }
 
 
 public bool  IsMoving (){
     return movement.isMoving;
 }
 
 public bool  IsJumping (){
     return jump.jumping;
 }
 
 public bool  IsTouchingCeiling (){
     return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
 }
 
 public Vector3  GetDirection (){
     return movement.direction;
 }
 
 public float  GetHangTime (){
     return movement.hangTime;
 }
 
 void  Reset (){
     gameObject.tag = "Player";
 }
 
 void  SetControllable ( bool controllable  ){
      canControl = controllable;
 }
 
 
 
 
 // Require a character controller to be attached to the same game object
 //@script RequireComponent (CharacterController)
 //@script AddComponentMenu ("2D Platformer/Platformer Controller")
 
 }
Comment
Add comment · Show 3
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 Unitraxx · Apr 22, 2013 at 01:18 AM 0
Share

Well it means that movement is null. Where do you initialize/assign a value to movement?

avatar image robertbu · Apr 22, 2013 at 01:30 AM 0
Share

It is hard to say how this problem should be fixed without seeing both (complete) scripts. It appears that 'movement' is not initialized. From what we see here, it is hard to see how it should be initialized.

avatar image Chris12345 · Apr 22, 2013 at 02:00 AM 0
Share

You are not referencing a object.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 22, 2013 at 04:15 AM

You declare movement on line 71, but you don't initialize it do it is NULL when you try and access it in Awake(). Since you made the variable public, I'm not sure how you want this handled. The solution may be as simple as:

 public PlatformerControllerMovement movement = new PlatformerControllerMovement(); 
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 MRollins · Apr 22, 2013 at 05:33 AM

THANKS! I'm a C# novice so I didn;t even notice it wasn't initialized and that would cause this error.

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

12 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

Related Questions

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Instantinate NullReferenceError problem 0 Answers

Trouble Instantiating Projectiles 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