简体   繁体   English

Unity3D在固定位置生成2d对象

[英]Unity3D spawning 2d objects at fixed locations

I have an object that when clicked it is destroyed and spawns randomly somewhere else on a timer. 我有一个对象,单击该对象时会被破坏,并在计时器的其他位置随机生成。 I'm trying to make it so instead of random spots it shows up at fixed locations. 我正在尝试使其固定位置显示而不是随机出现。

I also want them to randomly spawn at those fixed locations on a timed interval, one at a time.(so if it appears in one location for lets say 5 seconds, it will be destroyed and the next one will appear in a different location.) 我还希望它们以一定的时间间隔随机地在那些固定的位置生成,因此一次如果它出现在一个位置可以说5秒钟,它将被销毁,而下一个将出现在另一个位置。 )

I attempted to do fixed spawn locations, but the void spawner doesn't want to work. 我尝试进行固定的产卵位置,但是void产卵器不想工作。

I get a "The object of type "GameObject" has been destroyed but you are still trying to access it". 我收到“类型为“ GameObject”的对象已被破坏,但您仍在尝试访问它”。

I can fix this by commenting out the On_TouchStart destroy line, but I need it. 我可以通过注释掉On_TouchStart破坏行来解决此问题,但我需要它。

Here is my code: 这是我的代码:

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {

    public float AppearTime = 0f;
    public Transform[] teleport;
    public GameObject[] prefab;

    void Spawner(){ 
        int tele_num = Random.Range(0,5);
        int prefab_num = Random.Range(0,3);
        if (prefab !=null){
        Instantiate(prefab[prefab_num], teleport[tele_num].position, teleport[tele_num].rotation );
        }
    }


    void StartTime()
    {
        StartCoroutine(DoTime());

    }

    void OnEnable(){
        EasyTouch.On_TouchStart += On_TouchStart;
    }

    IEnumerator DoTime()
    {
        yield return new WaitForSeconds(AppearTime);
        Spawner();

    }

    void On_TouchStart (Gesture gesture){

        if (gesture.pickObject != null){
            Destroy(gesture.pickObject);
            StartTime();
        }
    }

If anyone could lead me on the right track I'd appreciate it. 如果有人能带领我走上正确的道路,我将不胜感激。

Thanks. 谢谢。

I figured it out. 我想到了。 Turns out the prefabs I were using were incorrect, so I needed to swap them out for ones that did. 原来我使用的预制件是不正确的,所以我需要将它们换成可用的预制件。 Thanks to the ones who helped. 感谢那些帮助过我们的人。

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

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