- Home /
Car NOS script not working
i am working on a car game but i am facing some scripting problems i am pretty new to unity so all the help is welcome using UnityEngine; using System.Collections;
public class NOS : MonoBehaviour {
var isReady : boolean = true;
var vehicle : GameObject;
var waitTime : float;
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("left shift") && isReady){
Nitrous();
}
}
function Nitrous(){
isReady = false;
vehicle.rigidbody.AddForce (transform.forward * 1000, ForceMode.Acceleration);
yield WaitForSeconds (waitTime);
isReady = true;
}
}
![alt text][1]
Errors [1]: /storage/temp/73741-title.jpeg
Answer by BloodBTF · Jul 10, 2016 at 12:06 AM
First of all, I see you're using C#, but your variables are in JavaScript format. (Var a : b). Change
var isReady : boolean = true;
var vehicle : GameObject;
var waitTime : float;
To this
public bool isReady = true;
public GameObject vehicle;
public float waitTime;
Also change
yield WaitForSeconds (waitTime);
To
yield return new WaitForSeconds (waitTime);
It's really just that simple
You can't use WaitForSeconds inside a normal function, it has to be inside an IEnumarator.
Answer by jcv8000 · Jul 10, 2016 at 03:33 PM
Converted the script completely to C# and changed the Nitrous function to a IEnumerator (Coroutine) so you can use WaitForSeconds. This should be completely usable.
using UnityEngine;
using System.Collections;
public class NOS : MonoBehaviour {
bool isReady = true;
GameObject vehicle;
float waitTime;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.LeftShift) && isReady) {
StartCoroutine(Nitrous());
}
}
IEnumerator Nitrous() {
isReady = false;
vehicle.GetComponent<Rigidbody>().AddForce (transform.forward * 1000, ForceMode.Acceleration);
yield return new WaitForSeconds(waitTime);
isReady = true;
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Guys i really need your help! Why i cant use unity addons? 0 Answers
SendMessage static parameters checking 0 Answers