简体   繁体   English

Unity产卵危害

[英]Unity Spawning hazards

I made a script to spawn object on platform randomly. 我编写了一个脚本,以在平台上随机生成对象。 在此处输入图片说明 , The spawning thing works fine, The second script is to disable the spawning objects(hazards) when they hit the platform. ,产卵的东西工作正常,第二个脚本是禁用产卵对象(危险)撞击平台时的功能。 They disapear like they should, but after a couple of hazards spawned, they keep spawning i see that in the console, but not on my Scene/Game screen. 他们会像预期的那样消失,但是在产生了几次危害之后,他们不断产生,我在控制台中看到了,但在我的“场景/游戏”屏幕上却没有看到。 在此处输入图片说明

The code for the spawning:` 产生代码:

using UnityEngine;
using System.Collections;

public class SpawnHazards : MonoBehaviour {

    #region Variables
    //Public
    //Private
    [SerializeField]
    private float minX = 0.0f;
    [SerializeField]
    private float maxX = 0.0f;
    [SerializeField]
    private GameObject[] hazards;
    [SerializeField]
    private float timeBetweenSpawns = 0.0f;
    private bool canSpawn = false;
    private int amountOfHazardsToSpawn = 0;
    private int hazardsToSpawn = 0;
    #endregion

    #region UnityFunctions
    void Start() {   
        canSpawn = true;
    }

    void Update() {    
        if(canSpawn == true)
        {
            StartCoroutine("GenerateHazard");
        }    
    }
    #endregion
    private IEnumerator GenerateHazard()
    {
        canSpawn = false;
        timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing values
        amountOfHazardsToSpawn = Random.Range(1, 6);  //Testing values

        for(int i =0; i < amountOfHazardsToSpawn; i++)
        {
            Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), 15.0f, 0.0f); // generate spawn position
            Instantiate(hazards[hazardsToSpawn], spawnPos, Quaternion.identity);  //spawn hazards
        }

        yield return new WaitForSeconds(timeBetweenSpawns);
        canSpawn = true;  
    }
}

the code for disabling them after collision: 碰撞后禁用它们的代码:

using System.Collections.Generic;
using UnityEngine;

public class HazardCollisionFunctions : MonoBehaviour {

    #region Variables
    //Public

    //Private
    #endregion

     #region UnityFunctions
    private void Start()
    {

    }

    private void Update()
    {

    }
    #endregion

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Platform")
        {
            this.gameObject.SetActive(false);
        }

        if (collision.gameObject.tag == "Player")
        {
            this.gameObject.SetActive(false);
        }
    }

}

I took the code you provided and put it in an empty Unity project. 我接受了您提供的代码,并将其放在一个空的Unity项目中。 I did not experience the cubes stopping spawning at all, so I feel like the issue may lie elsewhere. 我完全没有经历过立方体停止产卵的过程,所以我觉得问题可能出在其他地方。

I was able to simplify the looping of the coroutine for you though, which may avoid some issues in the future: 不过,我能够为您简化协程的循环,这可以避免将来出现某些问题:

private IEnumerator GenerateHazard()
{
    while (true)
    {
        timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing values
        amountOfHazardsToSpawn = Random.Range(1, 6);  //Testing values

        for (int i = 0; i < amountOfHazardsToSpawn; i++)
        {
            Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), 15.0f, 0.0f); // generate spawn position
            Instantiate(hazards[hazardsToSpawn], spawnPos, Quaternion.identity);  //spawn hazards
        }

        yield return new WaitForSeconds(timeBetweenSpawns);
    }
}

With this code, you only need to call StartCoroutine(GenerateHazard()); 使用此代码,您只需要调用StartCoroutine(GenerateHazard()); once in the Start function. Start功能中一次。

It is possible that the problem comes from calling the coroutine... I don't have Unity on my current PC, so I can't test it, but try this: 问题可能出在调用协程...我的当前PC上没有Unity,因此无法测试,但请尝试以下操作:

using UnityEngine;
using System.Collections;

public class SpawnHazards : MonoBehaviour {

    #region Variables
    //Public
    //Private
    [SerializeField]
    private float minX = 0.0f;
    [SerializeField]
    private float maxX = 0.0f;
    [SerializeField]
    private GameObject[] hazards;
    [SerializeField]
    private float timeBetweenSpawns = 0.0f;
    //private bool canSpawn = false;
    private int amountOfHazardsToSpawn = 0;
    private int hazardsToSpawn = 0;
    #endregion

    #region UnityFunctions
    void Start() {   
        //canSpawn = true;
    }

    void Update() {    
        timeBetweenSpawns -= Time.deltaTime;
        //if(canSpawn == true)
        if(timeBetweenSpawns < 0.0f)
        {
            //StartCoroutine("GenerateHazard");
            GenerateHazard();
        }    
    }
    #endregion
    private void GenerateHazard()
    {
        //canSpawn = false;
        timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing values
        amountOfHazardsToSpawn = Random.Range(1, 6);  //Testing values

        for(int i =0; i < amountOfHazardsToSpawn; i++)
        {
            Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), 15.0f, 0.0f); // generate spawn position
            Instantiate(hazards[hazardsToSpawn], spawnPos, Quaternion.identity);  //spawn hazards
        }

        //yield return new WaitForSeconds(timeBetweenSpawns);
        //canSpawn = true;  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM