- Home /
Prefab script values do not update
Hello all! So basically I have this random generator generates I prefab that I have at random times and at random positions between a range I have determined. So the problem is, I have so that if my score variable gets to a certain number, I want to update two other variables. However, this code only seems to update it for only 1 next game object being generated, but it does not permanently change the stats.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnerScript : killEnemy {
public GameObject enemy;
public Vector2 spawnValues;
public float spawnWait;
public int startWait;
public float spawnMostWait;
public float spawnLeastWait;
public bool terminate;
// Use this for initialization
void Start () {
StartCoroutine(Spawner());
}
// Update is called once per frame
public bool leveled;
void Update()
{
int[] levelNums = { 5, 10, 20, 30, 40, 50 };
for (int i = 0; i < 6; i++)
{
if (score == levelNums[i] && !leveled)
{
spawnMostWait += 1;
spawnLeastWait += 1;
leveled = true;
}
}
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
}
void CollectSavedValues()
{
score = PlayerPrefs.GetInt("exampleIntSave");
}
void CollectSavedValuesDead()
{
dead = PlayerPrefs.GetInt("exampleIntSave");
}
IEnumerator Spawner()
{
yield return new WaitForSeconds(startWait);
while (!terminate)
{
Vector2 spawnPosition = new Vector2(Random.Range(spawnValues.x-8, spawnValues.x), Random.Range(-spawnValues.y, spawnValues.y));
Instantiate(enemy, spawnPosition + Vector2.zero, gameObject.transform.rotation);
yield return new WaitForSeconds(spawnWait);
if(dead == 1)
{
terminate = true;
}
}
}
}
You should seriously reconsider how you've set this up. If you use this logic in the Update method, you're going to level up only once unless you remove the leveled variable. But if you take out that variable, your stats are going to become very high very quickly. The only time you need to check whether to increase the stats is when the score changes, so you should move your stat increase logic into whatever function increases the score. Likewise, rather than changing spawnWait every frame, change it ins$$anonymous$$d in the Spawner function right before/after you use it.
$$anonymous$$aking these changes may not solve your problem, but it will make your script much easier to debug in the future.
Answer by Legend_Bacon · Jan 21, 2019 at 05:51 PM
Hello there,
If by "Stats" you mean "spawnLeastWait" and "spawnMostWait", then you do have a logic error in there.
The bool "leveled" needs to be false to increase those variables by 1, however when that happens you immediately set it to true and never to false again.
That means that the stats will be at most increased ONCE, and then never again since "leveled" is never set to false.
I hope that helps!
Cheers,
~LegendBacon
Yeah so the issue with that is that when i do that, because it's the update method, it increases way too much since it checks once per frame, which is why I added that variable.
Answer by KISP · Jan 22, 2019 at 02:35 PM
You should seriously reconsider how you've set this up. If you use this logic in the Update method, you're going to level up only once unless you remove the leveled variable. But if you take out that variable, your stats are going to become very high very quickly. The only time you need to check whether to increase the stats is when the score changes, so you should move your stat increase logic into whatever function increases the score. Likewise, rather than changing spawnWait every frame, change it instead in the Spawner function right before/after you use it.
Making these changes may not solve your problem, but it will make your script much easier to debug in the future.
Your answer
Follow this Question
Related Questions
How To Random Range a Float? 2 Answers
Using Vector3 in ViewportToWOrldPoint 1 Answer
Random Spawn, Random Prefab 2 Answers
How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer
random place generator 1 Answer