Unity3D无法实例化

时间:2017-05-17 05:37:16

标签: unity3d scripting

public class SpawnerAI : MonoBehaviour
{
    //Aha Permintaan
    public GameObject[] Ahaprefab;
    GameObject Ahaprefabclone1,Ahaprefabclone2,Ahaprefabclone3,Ahaprefabclone4,Ahaprefabclone5;
    public Transform[] dupliposition;

    //artoolkit
    public GameObject ArtoolkitAc;
    GameObject au,ax,artoolkitAC;

    // Pergantian Permunculan AI
    public Camera[] cameras;
    public GameObject[] canvasall;

    public GameObject[] Aishown;
    public Transform[] aiposition;
    GameObject peopleclone1;
    GameObject peopleclone2;
    GameObject peopleclone3;
    GameObject peopleclone4;
    GameObject peopleclone5;

    private bool beingHandled = false;

    public bool pos1 = false;
    public bool pos2 = false;
    public bool pos3 = false;
    public bool pos4 = false;
    public bool pos5 = false;

    Animator Animdoor;

    public Vector3 Gcenter;
    public Vector3 TPosisi;
    RaycastHit hit;
    public bool touchedCh = false;

    void Start()
    {   
        cameras [0].enabled = true;
        cameras [1].enabled = false;
        canvasall [0].SetActive(true);
        canvasall [1].SetActive(false);
        Animdoor = GetComponent<Animator> ();
    }

    void Update () {
        if (/*some case  */ !beingHandled) 
        {
            StartCoroutine (HandleIt ());
        }
            if (Input.GetMouseButtonDown(0)) 
            {
                Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                if (Physics.Raycast (ray, out hit)) 
                {
                    peopleclone1= hit.collider.gameObject;
                    Gcenter = peopleclone1.transform.position;
                    peopleclone2= hit.collider.gameObject;
                    Gcenter = peopleclone2.transform.position;
                    peopleclone3= hit.collider.gameObject;
                    Gcenter = peopleclone3.transform.position;
                    peopleclone4= hit.collider.gameObject;
                    Gcenter = peopleclone4.transform.position;
                    peopleclone5= hit.collider.gameObject;
                    Gcenter = peopleclone5.transform.position;
                    TPosisi = Camera.main.ScreenToWorldPoint (Input.mousePosition);                 
                    touchedCh = true;
                }
            }
            if (Input.GetMouseButton (0)) {
                if (touchedCh) {
                    TPosisi = Camera.main.ScreenToWorldPoint (Input.mousePosition); 
                    cameras [0].enabled = false;
                    cameras [1].enabled = true;
                    canvasall [0].SetActive(false);
                    canvasall [1].SetActive(true);
                    //artoolkitAC.SetActive (true);
                    if (pos1==true) {
                        //pos1 = false;
                        Destroy(peopleclone1);
                    Destroy(GameObject.FindWithTag("Aha1"));
                        Debug.Log ("Posisi kesatu Kosong");
                    }
                    else if (pos2==true) {
                        //pos2 = false;
                        Destroy(peopleclone2);
                    Destroy(GameObject.FindWithTag("Aha2"));
                        Debug.Log ("Posisi kedua Kosong");
                    }
                    else if (pos3==true) {
                        //pos3 = false;
                        Destroy(peopleclone3);
                    Destroy(GameObject.FindWithTag("Aha3"));

                        Debug.Log ("Posisi ketiga Kosong");
                    }
                    else if (pos4==true) {
                        //pos4 = false;
                        Destroy(peopleclone4);
                    Destroy(GameObject.FindWithTag("Aha4"));
                    }
                    else if (pos5==true) {
                        //pos5 = false;
                        Destroy(peopleclone5);
                    Destroy(GameObject.FindWithTag("Aha5"));
                        Debug.Log ("Posisi kelima Kosong");
                    }

                }
            }
            if(Input.GetMouseButtonUp(0)){
                touchedCh= false;
            }
        //StartCoroutine(InfiniteLoop());
    }

    private IEnumerator HandleIt()
    {
        // AI
        beingHandled = true;
        int indAi = Random.Range (0, Aishown.Length);

        // process pre-yield
        Animdoor.SetInteger ("StateOP", 0);
        yield return new WaitForSeconds( 3.0f );
        if (pos1 == false) {
            // process post-yield
            yield return new WaitForSeconds( 8.0f );
            Animdoor.SetInteger ("StateOP", 1);
            GameObject.Find ("Bell").GetComponent<AudioSource> ().Play ();
            GameObject.Find ("doorclose").GetComponent<AudioSource>().Play ();
            peopleclone1 = Instantiate (Aishown [indAi], aiposition [0].position, Aishown [indAi].transform.rotation) as GameObject;
            yield return new WaitForSeconds( 2.0f );
            Ahaprefabclone1 = Instantiate (Ahaprefab[0], dupliposition [0].position, Quaternion.identity)as GameObject;
            beingHandled = false;
            pos1 = true;

            Debug.Log ("Posisi Pertama telah diisi");
            }
        else if (pos2 == false) {
            // process post-yield
            yield return new WaitForSeconds( 8.0f );
            Animdoor.SetInteger ("StateOP", 1);
            GameObject.Find ("Bell").GetComponent<AudioSource> ().Play ();
            GameObject.Find ("doorclose").GetComponent<AudioSource>().Play ();
            peopleclone2 = Instantiate (Aishown [indAi], aiposition [1].position, Aishown [indAi].transform.rotation) as GameObject;
            yield return new WaitForSeconds( 2.0f );
            Ahaprefabclone2 = Instantiate (Ahaprefab[1], dupliposition [1].position, Quaternion.identity)as GameObject;
            beingHandled = false;
            pos2 = true;
            Debug.Log ("Posisi Kedua telah diisi");
            }
        else if (pos3 == false) {
            // process post-yield
            yield return new WaitForSeconds( 8.0f );
            Animdoor.SetInteger ("StateOP", 1);
            GameObject.Find ("Bell").GetComponent<AudioSource> ().Play ();
            GameObject.Find ("doorclose").GetComponent<AudioSource>().Play ();
            peopleclone3 = Instantiate (Aishown [indAi], aiposition [2].position, Aishown [indAi].transform.rotation) as GameObject;
            yield return new WaitForSeconds( 2.0f );
            Ahaprefabclone3 = Instantiate (Ahaprefab[2], dupliposition [2].position,Quaternion.identity)as GameObject;
            beingHandled = false;
            pos3 = true;
            Debug.Log ("Posisi Ketiga telah diisi");
            }
        else if (pos4 == false) {
            // process post-yield
            yield return new WaitForSeconds( 8.0f );
            Animdoor.SetInteger ("StateOP", 1);
            GameObject.Find ("Bell").GetComponent<AudioSource> ().Play ();
            GameObject.Find ("doorclose").GetComponent<AudioSource>().Play ();
            peopleclone4 = Instantiate (Aishown [indAi], aiposition [3].position, Aishown [indAi].transform.rotation) as GameObject;
            yield return new WaitForSeconds( 2.0f );
            Ahaprefabclone4 = Instantiate (Ahaprefab[3], dupliposition [3].position, Quaternion.identity)as GameObject;
            beingHandled = false;
            pos4 = true;
            Debug.Log ("Posisi Keempat telah diisi");
            }
        else if (pos5 == false) {
            // process post-yield
            yield return new WaitForSeconds( 8.0f );
            Animdoor.SetInteger ("StateOP", 1);
            GameObject.Find ("Bell").GetComponent<AudioSource> ().Play ();
            GameObject.Find ("doorclose").GetComponent<AudioSource>().Play ();
            peopleclone5 = Instantiate (Aishown [indAi], aiposition [4].position, Aishown [indAi].transform.rotation) as GameObject;
            yield return new WaitForSeconds( 2.0f );
            Ahaprefabclone5 = Instantiate (Ahaprefab[4], dupliposition [4].position, Quaternion.identity)as GameObject;
            beingHandled = false;
            pos5 = true;
            Debug.Log ("Posisi Kelima telah diisi");
        }
        //artoolkitAC = Instantiate (ArtoolkitAc) as GameObject;
        //artoolkitAC.SetActive (false);
    }


    public void BacktoGameShop()
    {

        cameras [0].enabled = true;
        cameras [1].enabled = false;
        canvasall [0].SetActive(true);
        canvasall [1].SetActive(false);
    }

    // End Of code
}

我尝试了很多方法来销毁实例对象,但它不会被破坏。

  1. 首先我尝试了:
  2.   

    销毁(ahaprefab1);

    1. 第二次我尝试了标签:
    2.   

      销毁(GameObject.FindWithTag(&#34; AHA1&#34));

      它仍然不会破坏实例。但是当我将它用于Spawner Ai时。 Destroy(peopleclone1)会破坏实例。

      我坚持使用此代码。

1 个答案:

答案 0 :(得分:0)

好吧,建议首先检查你的对象是否只是null Debug.Log(peopleclone1)

如果它不为null,请尝试使用DestroyImmediate(peopleclone1),因为游戏对象没有被破坏的原因可能是因为他仍然在操作一些东西

即使DestroyImmediate不起作用,我也不知道该怎么做:/