- Home /
Unity fails when i press play
hi, i've been having this problem for a few days now and it's really anoying. i'm making this simple game and for now in the scene there are: a plane, a cube that spawns some spheres, a tower that does the same thing. the problem is: if i press play when all this stuff is in the scene, unity won't work: it completley freezes. instead if i press play when i put in the scene only the plane, and the cube that moves around with the arrow keys and that spawns spheres, it works perfectly fine. could this be a problem caused by my (very, very basic) scripts, even though it doesn't show any kind of error? thank you for your response
using UnityEngine;
using System.Collections;
public class Tower : $$anonymous$$onoBehaviour {
public GameObject towerBullet;
public Transform towerBulletSpawn;
[Range(1,5)]
public float waitTime;
private bool corutine = true;
// Use this for initialization
void Start () {
while (corutine == true)
StartCoroutine(Shoot());
}
// Update is called once per frame
void Update () {
}
IEnumerator Shoot ()
{
Instantiate(towerBullet, towerBulletSpawn.position, Quaternion.identity);
yield return new WaitForSeconds(waitTime);
Instantiate(towerBullet, towerBulletSpawn.position, Quaternion.identity);
}
}
you are right! this is my code. this means that i can't use loops? thank you once again
Could you clarify what you may want to happen if somewhere in the code you change corutine to false?
Answer by tanoshimi · Jun 07, 2015 at 09:52 PM
You can use loops. But they need to not block the main execution thread. So infinite loops in coroutines (with yields) are ok. I suspect what you meant to do was this:
using UnityEngine;
using System.Collections;
public class Tower : MonoBehaviour {
public GameObject towerBullet;
public Transform towerBulletSpawn;
[Range(1,5)]
public float waitTime;
private bool corutine = true;
// Use this for initialization
void Start () {
StartCoroutine(Shoot());
}
// Update is called once per frame
void Update () {
}
IEnumerator Shoot ()
{
while(true) {
Instantiate(towerBullet, towerBulletSpawn.position, Quaternion.identity);
yield return new WaitForSeconds(waitTime);
}
}
}
Thank you, i'll try that once again, because when I first tried it the bullets where spawned just a couple of times. Anyway I'all try again, thank you for your time!