Jump to content
Unity Insider Forum

unity is crashed


Someone

Recommended Posts

hi everyone

i use codes for spawn enemy in game 

but when i use a while circle for spawn unity is going to freeze and how many time i try to do it but all times unity freeze

its my codes 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyInfo : MonoBehaviour
{
    // rejebe spawn shodan
    public GameObject EnemyToSpawn;
    bool CanBeSpawn = true;
    
    public float EnemySpawnTime = 1;
    public float MaxY;
    public float StartingX;
    private float RandomX = 0.0f;

    // Move
    [Range(0f,15f)]
    public float MoveSpeed;

    void Start()
    {
        StartCoroutine(EnemySpawn());
    }

     
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        Move();
    }
    #region Respawn
    IEnumerator EnemySpawn()
    {

        
        SpawnEnemys();
        yield return new WaitForSeconds(EnemySpawnTime);

    }
    void SpawnEnemys()
    {
        RandomX = Random.Range(-MaxY, MaxY);
        while (CanBeSpawn == true)
        {
            Instantiate(EnemyToSpawn, new Vector3(RandomX, StartingX, 0), Quaternion.identity);
        }
        
    }
    #endregion Respawn

    #region Move
    void Move()
    {
        transform.Translate(-MoveSpeed * Time.deltaTime, 0, 0);
    }
    #endregion Move

    


}
 

Link zu diesem Kommentar
Auf anderen Seiten teilen

6 hours ago, Seyshiro said:

1. you should put the code into a Code-Block

2. you can find the english unity forum here

3. I'm assuming that the variable "CanBeSpawn" is never set to "false" and therefore the while loop never stops, so you have an endless loop and Unity freezes.

4. Sorry, my english is pretty bad

owkey but what is code-block !?

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...