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
1
Question by F3RILLA · Jul 23, 2013 at 09:08 PM · movementrunningsprinting

Sprinting Script Issue

Hi everyone,

I have an issue with my script, where I want the character to be able to move faster when a button is pressed. The script I wrote is based off a tutorial from brackeys (http://www.youtube.com/watch?v=wxdV4fZBEPQ). I have no idea why I'm getting the following error:

NullReferenceException: Object reference not set to an instance of an object Sprinting.Update () (at Assets/Scripts/Sprinting.js:31)

This is my script I've written in JavaScript. I think this could be a compatibility issue as the tutorial was written in Unity 3 and I am using Unity 4.

 #pragma strict
 
 var walkSpeed : float = 7; // Regular speed
 var sprintSpeed : float = 13; // Run speed
 private var charMotor : CharacterMotor;
 private var charController : CharacterController;
 
 function Start () 
 {
     charMotor = GetComponent(CharacterMotor);
     charController = GetComponent(CharacterController);
 }
 
 function Update () 
 {
     //Making the actual speed var
     var speed = walkSpeed;
     
     //Checking for oppertunity to sprint
     if ( charController.isGrounded && Input.GetKey("left shift"))
     {
         //changing the speed to sprint
         speed = sprintSpeed;
     }
     
     charMotor.movement.maxForwardSpeed = speed; //Setting the speed
 }


Thanks a lot for your help guys!

Comment
Add comment · Show 2
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 DoctorSauce · Jul 23, 2013 at 11:32 PM 0
Share

Your script only has 16 lines, and I think it says the error is on line 31. The error might be in a different script, could you put up the script called Sprinting? I may way off on that but it's worth a shot.

avatar image F3RILLA · Jul 23, 2013 at 11:50 PM 0
Share

The script above is for sprinting which has been coded in JavaScript and the code below is for the movement in C#:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 
 public class Character$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     
     public float moveSpeed = 10;
     public Vector3 moveDirection;
     public CharacterController character;
     public CollisionFlags collision;
     public float gravity = 10;
     
     // Use this for initialization
     void Start () {
         character = GetComponent<CharacterController>();
         moveDirection = Vector3.zero;
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetAxis("Horizontal") != 0)
         {
             transform.Rotate(0, Input.GetAxis("Horizontal"),0);
         }
         
         if(character.isGrounded){
             
             moveDirection = Vector3.forward * Input.GetAxis("Vertical");
             moveDirection = transform.TransformDirection(moveDirection).normalized;
             moveDirection *= moveSpeed;    
             
         }
         
         moveDirection.y -= gravity * Time.deltaTime;
                             
         collision = character.$$anonymous$$ove(moveDirection * Time.deltaTime);
 
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by clunk47 · Jul 24, 2013 at 01:54 AM

Be sure that the script is attached to your character. Be sure your character has a CharacterController component, AND the CharacterMotor component attached, which both come with the asset package. That seems to be the problem, because if you check for the components, you can see if they are null or not. Try this and see what you get in the console.

 #pragma strict
 
 var walkSpeed : float = 7; // Regular speed
 var sprintSpeed : float = 13; // Run speed
 private var charMotor : CharacterMotor;
 private var charController : CharacterController;
 
 function Start () 
 {
     charMotor = GetComponent(CharacterMotor);
     charController = GetComponent(CharacterController);
 }
 
 function Update () 
 {
     //Making the actual speed var
     var speed = walkSpeed;
     
     //Checking for oppertunity to sprint
     if(charController != null)
     {
         if ( charController.isGrounded && Input.GetKey("left shift"))
         {
             //changing the speed to sprint
             speed = sprintSpeed;
         }
     }
     else print("charController component not found.");
     
     if(charMotor != null)
         charMotor.movement.maxForwardSpeed = speed; //Setting the speed
     else print("charMotor component not found.");
 }
Comment
Add comment · Show 3 · 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 F3RILLA · Jul 24, 2013 at 12:29 PM 1
Share

Thanks, I forgot to add the Character $$anonymous$$otor and I will re-write the script you wrote for me to add the movement, thanks a lot clunk47

p.s. when I run the code, there was no errors :)

avatar image clunk47 · Jul 24, 2013 at 07:42 PM 1
Share

Always happy to help :D

avatar image JamesL98 · Nov 03, 2014 at 11:07 AM 0
Share

Im using Unity 4.5.5 And i have the same problem but ive tried all of your solutions and tried multiple scripts and something still isnt working? It comes up with the same error (not set to instance). ??? Have they changed what you have to type?

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

17 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

Related Questions

Player walks to the right? 1 Answer

What's wrong with my sprinting logic? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Player movement adding sprint, issues with rotation 0 Answers

How to stop character from running 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