- Home /
Question by
acabalifevo53 · Aug 30, 2021 at 02:42 PM ·
ienumeratorstop
Unity Stops Running
I write this line of code in order to put a limit to spawned objects in the scene but when "powerUpsOnPlatform" hit the maximum value unity stops responding.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUpSpawner : MonoBehaviour
{
[SerializeField] private GameObject[] powerUps;
private GameHandler gameHandler;
private float xBorder = 11.5f;
private float zBorder = 10;
private PlayerController playerController;
public int powerUpsOnPlatform;
void Start()
{
playerController = GameObject.Find("Player").GetComponent<PlayerController>();
gameHandler = GameObject.Find("GameHandler").GetComponent<GameHandler>();
StartCoroutine(PowerSpawner());
}
// Update is called once per frame
void Update()
{
/* if (powerUpsOnPlatform < 3)
{
StartCoroutine(PowerSpawner());
}*/
}
IEnumerator PowerSpawner()
{
while (!gameHandler.gameOver())
{
if (powerUpsOnPlatform < 3)
{
float spawnTime = Random.Range(5, 10);
powerUpsOnPlatform++;
yield return (new WaitForSeconds(spawnTime));
Instantiate(powerUps[0], GenerateRandomSpawnPosition(), Quaternion.identity);
}
}
}
private Vector3 GenerateRandomSpawnPosition()
{
float xBorders;
float zBorders;
xBorders = Random.Range(-xBorder, xBorder);
zBorders = Random.Range(-zBorder, zBorder);
Vector3 spawnPos = new Vector3(xBorders, 0, zBorders);
return spawnPos;
}
public void PowerUpDecreaser()
{
powerUpsOnPlatform--;
}
}
Comment
Answer by acabalifevo53 · Aug 30, 2021 at 03:16 PM
Oh guys I found it, It's because I did not return anything if the "if statement is not correct." and that leads to game crush.But it is still wierd that Unity did not warn me about that.
Your answer
Follow this Question
Related Questions
Coroutine start and stop 2 Answers
Why is this method being called multiple times? 0 Answers
How do I solve a jerk in movement? 2 Answers
Generating Objects at High Speeds 0 Answers
How to declare an Action with return type other than void? 2 Answers