产生的物体不会再次产卵

时间:2016-12-10 09:27:03

标签: c# unity3d

我在同一场景中拥有多个区域的手机游戏中工作。每个区域都有一个触发器,当玩家进入该区域时,会生成几个对象来拾取它们。如何停用拾取的物体,以便当玩家再次进入此区域时,此物体不会再生成?

这是我的代码:

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

public class InOutZone_ZONAS: MonoBehaviour {

//Objetos
[Header("Objetos")]
public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;
private GameObject[] despawnObjects;


void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        SpawnObjectsZ ();
    }
}
void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
        for (int i = 0; i < despawnObjects.Length; i++)
        {
            PoolManager.Pools ["Objetos"].Despawn (despawnObjects[i].transform);
            Debug.Log("Despawnea Objetos");
        }
    }
}
void SpawnObjectsZ()
{
    foreach (GameObject spawnPosition in spawnPositions) {
        int selection = Random.Range (0, spawnObjects.Count);
        PoolManager.Pools ["Objetos"].Spawn (spawnObjects [selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
    }
}
}

1 个答案:

答案 0 :(得分:0)

  

如何在播放器时停用拾取的对象   再次进入此区域此对象不再生成?

有很多方法可以做到这一点。在使用List

生成GameObject之后,您可以从spawnObjects.Remove(spawnObjects[selection]);中删除GameObject
public class Playervitals : MonoBehaviour
{
    //Objetos
    [Header("Objetos")]
    public List<GameObject> spawnPositions;
    public List<GameObject> spawnObjects;
    private GameObject[] despawnObjects;


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            SpawnObjectsZ();
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
            for (int i = 0; i < despawnObjects.Length; i++)
            {
                PoolManager.Pools["Objetos"].Despawn(despawnObjects[i].transform);
                Debug.Log("Despawnea Objetos");
            }
        }
    }

    void SpawnObjectsZ()
    {
        for (int i = 0; i < spawnPositions.Count; i++)
        {
            GameObject spawnPosition = spawnPositions[i];
            int selection = Random.Range(0, spawnObjects.Count);
            PoolManager.Pools["Objetos"].Spawn(spawnObjects[selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
            spawnObjects.Remove(spawnObjects[selection]);
        }
    }
}