Hey all
I’m working on a game where you can drink a coffee and run faster for a period of time, but for some reason I get an error in every frame when running the game, until I collects a coffee.
This is the error-messege:
NullReferenceException: Object reference not set to an instance of an object
collect.Update () (at Assets/Scripts/collect.cs:35)
And this is my code:
using UnityEngine;
using System.Collections;
using System;
public class collect : MonoBehaviour {
private float walkSpeed = 7; //regular speed
private float runSpeed = 20; //run speed
private double timer;
private CharacterMotor chMotor;
private float height; //initial height
private float speed;
void OnTriggerEnter(Collider other){
if(other.tag == "cofee")
{
timer = 20;
Destroy(other.gameObject);
}
}
// Use this for initialization
void Start () {
timer = -1;
chMotor = GetComponent<CharacterMotor>();
}
// Update is called once per frame
void Update () {
if (timer < 0 )
{
chMotor.movement.maxForwardSpeed = walkSpeed; // set max speed
}
else
{
timer -= Time.deltaTime;
chMotor.movement.maxForwardSpeed = runSpeed; // set max speed
}
}
}
Can someone plz tell WTF is wrong?