Value set in function doesn't update the glabal value
So I'm making the space shooter tutorial game and I was mockin around a bit to see if I could create a system where the asteroids would coma faster at you after a set amount of waves, however.. I'm now completely stuck on a piece of code that just doesn't want to work...
So I made a few variables that count down how many waves have appeared and when a certain limit has been reached I want it to update the speed which I did like so
public class GameController : MonoBehaviour {
// bunch of other variables
public Mover spd
public int speedRounds
private int speedCount
IEnumerator SpawnWaves (){
yield return new WaitForSeconds (startWait);
while (true) {
if (speedCount < speedRounds) {
speedCount++;
}
if (speedCount == speedRounds) {
speedCount = 0;
spd.Faster ();
}
//code for making the asteroids spawn
following up with the following mover script where Faster(); is calling to
public class Mover : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float maxSpeed;
private float speedCalc;
private float maxSpeedCalc;
float speedMin;
float maxSpeedMin;
void Start () {
Speed ();
rb = GetComponent<Rigidbody> ();
rb.velocity = transform.forward*speedCalc;
//Debug.Log (speedCalc+ "," + speed+ ", " + speedMin);
}
public void Faster () {
if (maxSpeedCalc > 0) {
maxSpeedMin++;
speedMin++;
Speed ();
}
else {
return;
}
}
void Speed (){
maxSpeedCalc = maxSpeed - maxSpeedMin;
speedCalc = speed - speedMin;
}
public void Reset (){
speedMin = 0;
maxSpeedMin = 0;
}
}
The problem is that, for some reason that I can't understand, it will update speedMin inside the void Faster () but nowhere else. Whenever I debug inside Faster () it will steadily count up the speedMin variable (which is also why I needed to put in the Reset () function which is called in MasterController's Start () so that they will reset when the game begins). However, when I debug anywhere else in the code it tells me that speedMin is still 0...
I'm pretty new to Unity (hence the tutorial) but I have some basic programming knowledge so I normally have a pretty good idea of what I can and cannot do but this just baffles me..
If anyone can help it would be most appreciated!
then either Reset is constantly called, you debug the values on different instances of the script then the one the increase is performed on, or the value is reset somewhere else not shown
Your answer
Follow this Question
Related Questions
space shooter tutorial issue "Ending the game" unity 2018.2 b 1 Answer
How to instantiate (MonoBehaviour) script multiple times and call void? 1 Answer
Underwater Effect Help 1 Answer
Creating a shopping cart in Unity scripts problems 0 Answers
Entire contents of public void skipped except for one line 1 Answer