speed power ups - error

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?

I’ve looked through it and don’t see anything wrong with it. It seems to be having an issue with the charactermotor script, or perhaps the movement variable inside that script. I’d start looking at those to see what’s going on.

I have an error every frame until I have coffee too. That’s just my standard morning. Also, this should really be in the Scripting forum; you’ll get a lot more help there.

thx for the input I will try looking into it and perhaps move it to script forum.

best response ever :slight_smile: