- Home /
Problem with increasing rate of prefab instantiation
I'm trying to make a game in unity in which you have to dodge rocks falling from above you. I have copied the code i have used below but the problems i am facing are that the if condition- if(ScoreScript.currentScore%10==0) is behaving weirdly. Like the value of spawnRate will decrease as long as mod 10 is done, as soon as i change it to 100, the modulus doesnt work and sometimes spawnRate will decrease at currentScore value at 30 or 40 instead of at 20.
Code:- #pragma strict
var spawnPosition : Vector3;
var Boulder : GameObject;
var spawnRate : float = 0.5;
var nextSpawn : float = 1;
var count : float;
function Start () {
}
function Update () {
count = ScoreScript.currentScore%100;
if(Time.time > nextSpawn){
Spawn();
nextSpawn = Time.time + spawnRate;
if(ScoreScript.currentScore%10==0&&spawnRate>=0.1){
spawnRate-= 0.1;
}
}
}
function Spawn(){
spawnPosition = new Vector3(
Random.Range(collider.bounds.min.x,collider.bounds.max.x),185.4661,563.0046);
Instantiate(Boulder, spawnPosition,Quaternion.identity);
}
Oh forgot to mention this, The modulus works most of the time(I checked this) but it has no effect whatsoever on the spawnRate, it won't go down for some reason. I think theres something wrong with the if condition but i just can't figure it out :/
What is ScoreScript.currentScore
? What sorts of values might we expect it to have?
currentScore is a variable that i'm importing from ScoreScript. It is a static variable.
It starts running from 0 till well how so ever the game runs.
Answer by vinod.kapoor · May 07, 2014 at 10:08 AM
may be your current score is declared as float and ScoreScript.currentScore%10==0 may not be returning true. please Debug the value of ScoreScript.currentScore%10 and check if it is the reason.
I did add a variable to check the values ScoreScript.currentScore%10 ==0 returns, and it is returning all the correct values with a 0 at the right values. It returns 0 at 10 and 20 and so on.
Never$$anonymous$$d, i managed to work around this problem :)
Your answer
Follow this Question
Related Questions
Timer For Occasional Action 2 Answers
Display GUI when kill count reaches 5 1 Answer
"tag" & "if condition with OnTriggerEnter 2 Answers
|| inside if statement not working. (noob) 3 Answers
comparing two materials 0 Answers