我试图创建一个脚本(在javascript中,但最好是C#),以执行以下步骤:
(1)创建主摄像机的克隆; (1)从编辑器中的数组集中实例化预制件; (2)为所有这些实例化的游戏对象设置一个标签(同一个); (3)等待1秒; (4)销毁保存(2)中设置的标签的每个游戏对象,并销毁克隆的摄像机。
我提出了以下代码,但它不起作用。有人可以帮忙吗?
代码已更新
using UnityEngine;
using System.Collections;
public class PreLoader : MonoBehaviour {
//Array of Objects To Spawn
public GameObject[] objectsToSpawn;
// Use this for initialization
void Start ()
{
StartCoroutine(SpawnItemsFromArray());
}
IEnumerator SpawnItemsFromArray() //You can name this however you like
{
//Instantiates another camera as a GameObject (easy access to Components and removal of Camera)
GameObject cam1 = Instantiate(GameObject.FindWithTag("MainCamera"), Vector3.zero, Quaternion.FromToRotation(new Vector3(0, 0, 0), new Vector3(0, 0, 1))) as GameObject;
//Gets AudioListener Component and disables it
cam1.GetComponent<AudioListener>().enabled = false;
float space = 1;
float originalSpace = -3;
for (int i = 0; i < objectsToSpawn.Length; i++)
{
//You'll have to Instantiate as a GameObject to change its tag
GameObject go = Instantiate(objectsToSpawn[i], new Vector3(originalSpace, 0, 10.0f), transform.rotation) as GameObject;
originalSpace += space;
//Changes the tag to "Holder"
go.tag = "Holder";
//This will change the tag of the GameObject that this script is attached too
//gameObject.tag = "Holder";
}
//Waits for 2 Sec
yield return new WaitForSeconds(2);
//This will destroy (new Camera) and all that have tag "Holder"
GameObject[] goDestroy = GameObject.FindGameObjectsWithTag("Holder");
foreach (GameObject goHolder in goDestroy) Destroy(goHolder);
Destroy(cam1);
}
}
答案 0 :(得分:2)
据我所知yield WaitForSeconds(2);
仅适用于IEnumerator
。用StartCoroutine(**IEnumeratorMethodName**());
您要求提供C#代码,所以我希望这会有所帮助:(祝你好运)
void Start ()
{
StartCoroutine(SpawnItemsFromArray());
}
IEnumerator SpawnItemsFromArray() //You can name this however you like
{
//Instantiates another camera as a GameObject (easy access to Components and removal of Camera)
GameObject cam1 = Instantiate(GameObject.FindWithTag("MainCamera"), Vector3.zero, Quaternion.FromToRotation(new Vector3(0, 0, 0), new Vector3(0, 0, 1))) as GameObject;
//Gets AudioListener Component and disables it
cam1.GetComponent<AudioListener>().enabled = false;
//If you added this script to the Main Camera uncomment line below and add the appropriate script name to it
//cam1.GetComponent<**ScriptName**>().enabled = false; //This will make sure the new instantiated Camera doesn't run this script (Otherwise lots of cameras/GameObjects will be spawned
float space = 1;
float originalSpace = -3;
for (int i = 0; i < objectsToSpawn.Length; i++)
{
//You'll have to Instantiate as a GameObject to change its tag
GameObject go = Instantiate(objectsToSpawn[i], new Vector3(originalSpace, 0, 10.0f), transform.rotation) as GameObject;
originalSpace += space;
//Changes the tag to "Holder"
go.tag = "Holder";
//This will change the tag of the GameObject that this script is attached too
//gameObject.tag = "Holder";
}
//Waits for 2 Sec
yield return new WaitForSeconds(2);
//This will destroy (new Camera) and all that have tag "Holder"
GameObject[] goDestroy = GameObject.FindGameObjectsWithTag("Holder");
foreach (GameObject goHolder in goDestroy) Destroy(goHolder);
Destroy(cam1);
}