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 GothicSyn · Mar 03, 2014 at 02:21 AM · errorreferencenull

Null Reference Annoying Error

So I have this script running and it has no errors but when the game is run it simply kicks back

NullReferenceException

AdvancedMovement.SetUp () (at Assets/Scripts/AdvancedMovement.cs:90) AdvancedMovement+c__Iterator0.MoveNext () (at Assets/Scripts/AdvancedMovement.cs:69)

Its supposed to allow me to control my character while keeping the code simple enough to re-used without rewriting it every time I want to make something move

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class AdvancedMovement : MonoBehaviour {
     public enum State{
         Idle,
         Init,
         Setup,
         Run
     }
 
     public enum Turn{
         left = -1,
         none = 0,
         right = 1
     }
     public enum Forward{
         back = -1,
         none = 0,
         forward = 1
     }
 
     public float RotationalSpeed = 250;            // The speed the player turns
     public float strafeSpeed = 5f;                // The speed the player strafes
     public float walkSpeed = 10;                // The speed the player walks
     public float runSpeed = 2;                    // The speed the player will run at
     public float gravity = 10;                    // A variable for the downward forces of the game
     public float airTime = 0;                    // The set amount of time since the player last touched the ground
     public float fallTime = .5f;                // Amount fo time before a drop is considered a fall
     public float jumpHeight = 2;                // The set height a player can jump
     public float jumpTime = 1.5f;                // The length of time it takes to eecute a jump
 
     public CollisionFlags _collisionFlags;        // Collisions detectec by this variable
     private Vector3 _moveDirection;                // Vector 3 to control the direction a player is moving
     private Transform _myTransform;                // Cached transform for perfomance purposes
     private CharacterController _controller;    // Cached version of the Character Controller
 
     private Turn _turn;
     private Forward _forward;
     private Turn _strafe;
     private bool _run;
     private bool _jump;
 
     private State _state;
 
     public void Awake(){
         _myTransform = transform;
         _controller = GetComponent<CharacterController>();
 
         _state = AdvancedMovement.State.Init;
     }
 
     // Use this for initialization
     IEnumerator Start () {
         while(true){
             switch(_state){
             case State.Init:
                 Init();
                 break;
             case State.Setup:
                 SetUp();
                 break;
             case State.Run:
                 ActionCommands();
                 break;
             }
 
             yield return null;
         }
     }
 
     private void Init(){
         if(!GetComponent<CharacterController>()) return;
         if(!GetComponent<Animation>()) return;
 
         _state = AdvancedMovement.State.Setup;
     }
     private void SetUp(){
         _moveDirection = Vector3.zero;
         animation.Stop();                                // Halt any animatiosn set to play automatically
         animation.wrapMode = WrapMode.Loop;                // Loop animations continuosly
         animation["jump"].layer = -1;
         animation["jump"].wrapMode = WrapMode.Once;
         animation.Play ("idle");                        // Set animation to idle on script and game start
 
         _turn = AdvancedMovement.Turn.none;
         _forward = AdvancedMovement.Forward.none;
         _strafe = AdvancedMovement.Turn.none;
         _run = false;
         _jump = false;
 
         _state = AdvancedMovement.State.Run;
     }
 
     private void ActionCommands(){
         // Allows for left/right movement
             _myTransform.Rotate(0, (int)_turn * Time.deltaTime * RotationalSpeed, 0);
 
         // Checks to see if the player grounded
         if(_controller.isGrounded){
             airTime = 0;
             
             _moveDirection = new Vector3((int)_strafe, 0, (int)_forward);
             _moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;
             _moveDirection *= walkSpeed;
 
             // Allows movement forward and backward
             if(_forward != Forward.none){
                 if(_run){                                // Checks for run command 
                     _moveDirection *= runSpeed;            // Sets the run speed
                     Run ();                                // Plays the run animation
                 }
                 else{
                     Walk ();                            // Plasy the walk animation
                 }
             }
             else if(_strafe != AdvancedMovement.Turn.none){            // Checks for the strafe command
                 Strafe ();
             }
             else{
                 Idle();                                    // If nothing is active return to idle mode
             }
             
             // Controls the Jump animation
             if(_jump){
                 if(airTime < jumpTime){
                     _moveDirection.y += jumpHeight;
                     Jump();
                 }
             }
         }
         else{
             //If we have detected collisons
             if((_collisionFlags & CollisionFlags.CollidedBelow) == 0){
                 airTime += Time.deltaTime;        // Increase the airtime
                 if(airTime > fallTime){            // If character been airborne too long
                 Fall();                            // play the fall animation
                 }
             }
         }
         
         _moveDirection.y -= gravity * Time.deltaTime;
         //Find and store collisions
         _collisionFlags = _controller.Move (_moveDirection * Time.deltaTime);
     }
 
     public void MoveMeForward(Forward z){
         _forward = z;
     }
     public void Turning(Turn y){
         _turn = y;
     }
     public void Strafe(Turn x){
         _strafe = x;
     }
     public void ToggleRun(){
         _run = !_run;
     }
     public void JumpUp(){
         _jump = true;
     }
 
 // Selected animations that will be executed when called by the secondary scripts
     public void Idle(){
         animation.CrossFade("idle");
     }
 
     public void Walk(){
         animation.CrossFade("walk");
     }
 
     public void Run(){
         animation["walk"].speed = 1.5f;
         animation.CrossFade("walk");
     }
 
     public void Strafe(){
         animation.CrossFade("walk");
     }
 
     public void Jump(){
         animation.CrossFade("jump");
     }
 
     public void Fall(){
         animation.CrossFade("fall");
     }
 }

I've been through the code half a dozen times but still cant see why it kicks back the null reference any help guys? I have the character controller and my PlayerInput scripts attached to my character so I'm not missing anything its in this script somewhere I hope

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

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

20 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

Related Questions

Despawner script returns null reference exception. 0 Answers

Simple Script getting Errors. 1 Answer

How do I fix this Null Reference error and update my score? 2 Answers

NullReferenceException Error 6 Answers

NullReferenceException: Object reference not set to an instance of an object 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