- Home /
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!
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.
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);
}
}
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.");
}
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 :)
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
Follow this Question
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