- Home /
Variable wont change when I change it in script
Hi, so I just asked a previous question similar to this one, I'm sorry but not sure how to solve this one either. So I have an upgrade where the spawnTime decreases when you purchase it. Script :
public Catch points;
public SpawnItems spawnTime;
public float cost;
public int count;
private float baseCost;
public Text text;
void Start ()
{
baseCost = cost;
}
public void PurchasedItem ()
{
if (points.points >= cost) {
points.points -= cost;
points.text.text = points.points.ToString () + " POINTS";
spawnTime.spawnTime -= 4f;
count += 1;
cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count));
}
}
and the other SpawnItems script looks like so:
public Transform[] SpawnPoints;
public float spawnTime = 5f;
public GameObject Collectables;
void Start ()
{
InvokeRepeating ("SpawnCollectables", spawnTime, spawnTime);
}
void SpawnCollectables()
{
int spawnindex = Random.Range (0, SpawnPoints.Length);
Instantiate (Collectables, SpawnPoints [spawnindex].position, SpawnPoints [spawnindex].rotation);
}
So the problem is when I buy the upgrade that reduces the spawnTime does nothing, the reason why is because in the SpawnItems script I have the void Start () which is only run once and it does not update. So I need help on how to update the spawnTime in the SpawnItems script once you buy the upgrade. I tried void Update () but that ends up spawning a bunch of collectables.
Answer by MT369MT · Sep 19, 2017 at 07:34 PM
Hi I don't know how to do it but here is a solution without InvokeRepeating.
public float Counter;
void Update()
{
Counter += Time.deltaTime;
if (Counter >= spawnTime)
{
Instantiate...
Counter = 0;
}
}