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;
}
}
Errors
2 Answers
2
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
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;
}
}
You can't use WaitForSeconds inside a normal function, it has to be inside an IEnumarator.
– Whiteleaf