Difficulty parameter not correctly affecting spawn rate on button click
I've been stuck on this problem for about a week and am at a loss of how to resolve it. I am trying to speed up the instantiate rate of the enemies when the difficulty buttons on the start screen are clicked. In order to do this I've set a public difficulty variable in my DifficultyButton script and set it in the inspector to 1-3 depending on the button, and I'm trying to divide the spawnIntervalEnemy float from my SpawnManager script by that variable inside my GameManager script in order to make them spawn at different rates when the corresponding button is clicked. There seems to be no errors, but all 3 buttons keep the enemies at the same spawn rate no matter which one I click. I'm having to pull variables from multiple different scripts so idk if that's what's causing the issue or what, and I've tried multiple different solutions but nothing seems to do the trick and make the buttons spawn enemies at the correct rate, so any help is appreciated. Here are my 3 scripts with the relevant code, hopefully this helps.
public class DifficultyButton : MonoBehaviour
{
private Button button;
private GameManager gameManager;
public int difficulty;
// Start is called before the first frame updateX
void Start()
{
button = GetComponent<Button>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
button.onClick.AddListener(SetDifficulty);
}
// Update is called once per frame
void Update()
{
}
void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked");
gameManager.StartGame(difficulty);
}
-------------------------------------------------------------------------------------------------
public class GameManager : MonoBehaviour
{
public TextMeshProUGUI scoreText;
public TextMeshProUGUI gameOverText;
public static bool isGameActive;
public Button restartButton;
public SpawnManager other;
public GameObject titleScreen;
private int score;
// Start is called before the first frame updateX
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// Updates Score
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "Score: " + score;
}
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
restartButton.gameObject.SetActive(true);
isGameActive = false;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void StartGame(int difficulty)
{
isGameActive = true;
// Starts score at 0
score = 0;
UpdateScore(0);
other.InvokeObjects();
other.spawnIntervalEnemy /= difficulty;
titleScreen.gameObject.SetActive(false);
}
-------------------------------------------------------------------------------------------------
public class SpawnManager : MonoBehaviour
{
public GameObject[] enemies;
public GameObject[] obstacles;
public GameObject[] powerup;
private float spawnPosZ = 15;
private float spawnRangeX = 10;
private float startDelay = 2;
public float spawnIntervalEnemy = 1.5f;
private float spawnIntervalObstacle = 1.0f;
private float spawnIntervalPowerup = 5.0f;
// Start is called before the first frame updateX
void Start()
{
InvokeObjects();
}
// Update is called once per frame
void Update()
{
if (!GameManager.isGameActive)
{
CancelInvoke("SpawnRandomEnemy");
CancelInvoke("SpawnRandomObstacle");
CancelInvoke("SpawnPowerup");
enabled = false;
}
}
void SpawnRandomEnemy()
{
int enemyIndex = Random.Range(0, enemies.Length);
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0.75f, spawnPosZ);
Instantiate(enemies[enemyIndex], spawnPos, enemies[enemyIndex].transform.rotation);
}
void SpawnRandomObstacle()
{
int obstacleIndex = Random.Range(0, obstacles.Length);
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), .5f, spawnPosZ);
Instantiate(obstacles[obstacleIndex], spawnPos, obstacles[obstacleIndex].transform.rotation);
}
void SpawnPowerup()
{
int powerupIndex = Random.Range(0, powerup.Length);
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), .75f, spawnPosZ);
Instantiate(powerup[powerupIndex], spawnPos, powerup[powerupIndex].transform.rotation);
}
public void InvokeObjects()
{
InvokeRepeating("SpawnRandomEnemy", startDelay, spawnIntervalEnemy);
InvokeRepeating("SpawnRandomObstacle", startDelay, spawnIntervalObstacle);
InvokeRepeating("SpawnPowerup", startDelay, spawnIntervalPowerup);
}
}
Answer by LoricGarde · Oct 28, 2021 at 10:50 AM
Have you tried making "spawnIntervalEnemy" private, and changing it's value with a public void method? You could add a Debug.Log to check that you ACTUALLY call this method, then print the difficulty variable and the spawnIntervalEnemy after the division is made. Something like:
public class SpawnManager : MonoBehaviour
{
private float spawnIntervalEnemy;
private void Start()
{
spawnIntervalEnemy = 1.5f;
}
public void SetDifficulty(float difficulty)
{
spawnIntervalEnemy=spawnIntervalEnemy/difficulty;
Debug.Log("difficulty: "+ difficulty +" new value: "+spawnIntervalEnemy);
}
}
Thank you for your response, but I'm not exactly sure what you mean as you're combining 3 different scripts see$$anonymous$$gly into one in your response. The spawnIntervalEnemy is from SpawnManager, the SetDifficulty void is in the DifficultyButton, and I need the difficulty to change in the StartGame method in my GameManager script so the difficulty is called at the button click at the start of the game. Sorry if my original post wasn't clear, hopefully this clears it up because I'd really like your input, but your comment is kinda confusing.
Hi, I'm not combining different scripts, just modifying the spawnmanager. The names of the methods can be the same, you can change them if you want. When you call "other.spawnIntervalEnemy /= difficulty;" in StartGame, you're modifying a public variable. What I'm suggesting is storing that variable as private and modifying it in a method.
I think I see what you're saying. Problem is I need the SetDifficulty method to be in the DifficultyButton script so it's called on button click, so it can't be in the SpawnManager. This is my fault for not providing the relevant info, my SpawnManager actually has significantly more code but I didn't want to flood the post with it, but for clarity's sake I have updated the SpawnManager script to hopefully clear up this confusion. Hopefully with this cleared up we can work towards a solution because I greatly value you taking the time to help me and want to make the most of your time and efforts.