Unity产生危害

时间:2018-04-18 11:52:18

标签: c# unity3d

我制作了一个脚本,在平台上随机生成对象。enter image description here,产生的东西很好,第二个脚本是当它们撞到平台时禁用产卵对象(危险)。它们会像它们应该的那样消失,但是在产生了几个危险后,它们会继续产卵,我会在控制台中看到它,但不会出现在我的场景/游戏屏幕上。 enter image description here

产卵代码:`

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;  
    }
}

碰撞后禁用它们的代码:

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);
        }
    }

}

2 个答案:

答案 0 :(得分:1)

我把你提供的代码放在一个空的Unity项目中。我没有经历过立方体停止产卵,所以我觉得问题可能在其他地方。

我能够为你简化协程的循环,这可能会避免将来出现一些问题:

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);
    }
}

使用此代码,您只需在StartCoroutine(GenerateHazard());函数中调用Start一次。

答案 1 :(得分:0)

问题可能来自调用协程... 我目前的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;  
    }
}