ok so as soon as i press “p” to increase the difficulty, difficulty increases to 2 and the timer sets to 0. i already tried making it a float but that didn’t change anything?
var xPos : float = Random.Range(-50,50);
var yPos : float = Random.Range(-50,50);
var zPos : float = Random.Range(69,9001);
var spawnPos : Transform;
var spawnObject : GameObject;
var timer : float = 1;
var difficulty = 1;
var score = 0;
var countScore = 1;
function Start () {
Invoke("CustomUpdate",timer);
}
function CustomUpdate () {
Invoke("CustomUpdate",timer);
xPos = Random.Range(-50,50);
yPos = Random.Range(-50,50);
zPos = Random.Range(69,9001);
spawnPos.position.x = xPos;
spawnPos.position.y = yPos;
spawnPos.position.z = zPos;
Instantiate(spawnObject,spawnPos.position,spawnPos.rotation);
if(countScore == 1){
score += 1;
}
}
function Update () {
if(Input.GetKeyDown("o")){
if(difficulty >= 2){
difficulty -= 1;
}
}
if(Input.GetKeyDown("p")){
if(difficulty <= 9){
difficulty += 1;
}
}
timer = 1 / difficulty;
}
function Message () {
countScore = 0;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 200, 40),"score:" + score);
GUI.Label (Rect (10, 60, 200, 40),"difficulty" + difficulty);
}
Don't declare fields as 'var', limited that for being snazy inside functions. It will help you avoid doing logical mistakes. Now you have to 'guess' (since you seem unsure) what type of the result '1 / difficulty' will be. It is a int div by an int, the result will be an int. Hence, 0.
– Zarkow