Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在unity中销毁和创建相同的游戏对象_C#_Unity3d_Unity5 - Fatal编程技术网

C# 如何在unity中销毁和创建相同的游戏对象

C# 如何在unity中销毁和创建相同的游戏对象,c#,unity3d,unity5,C#,Unity3d,Unity5,我试图移动我的玩家来消灭怪物,第一次消灭怪物时,我出现以下错误: MissingReferenceException:类型为“GameObject”的对象已被销毁,但您仍在尝试访问它。 您的脚本应该检查它是否为null,或者不应该销毁对象 我怎样才能使它复制自己成为一个新的游戏对象 这是我的剧本: using System.Collections.Generic; using UnityEngine; public class TimeMap: MonoBehaviour { publ

我试图移动我的玩家来消灭怪物,第一次消灭怪物时,我出现以下错误:

MissingReferenceException:类型为“GameObject”的对象已被销毁,但您仍在尝试访问它。 您的脚本应该检查它是否为null,或者不应该销毁对象

我怎样才能使它复制自己成为一个新的游戏对象

这是我的剧本:

using System.Collections.Generic;
using UnityEngine;

public class TimeMap: MonoBehaviour {
    public GameObject selectedUnit;

    public GameObject selectedMonster;
    public TileType[] tileTypes;

    try{
        if(selectedMonster.GetComponent<Monster>().tileX == selectedUnit.GetComponent<Unit>().tileX && selectedMonster.getComponent<Monsters>().tileY == selectedUnit..GetComponent<Unit>())
            Destroy(selectedMonster);

    }

    public void SpawnMon() {
        if(counter % 5 == 0){
            Debug.Log(" counter % 5 == 0");
            Instantiate(selectedMonster.GetComponent<Monsters>().Monster, new Vector3(Random.Range(1,8), Random.Range(1,8), -1), Quaternion.identity);
        }
    }
}
使用System.Collections.Generic;
使用UnityEngine;
公共类时间映射:单行为{
公共游戏对象选择单元;
公共游戏对象选择Monster;
公共TileType[]TileType;
试一试{
如果(selectedMonster.GetComponent().tileX==selectedUnit.GetComponent().tileX&&selectedMonster.GetComponent().tileY==selectedUnit..GetComponent())
销毁(selectedMonster);
}
public-void-mon(){
如果(计数器%5==0){
Log(“计数器%5==0”);
实例化(selectedMonster.GetComponent().Monster,新向量3(Random.Range(1,8),Random.Range(1,8),-1),Quaternion.identity);
}
}
}

因此,我尝试每5步创建一个相同的对象或其克隆,但在执行此操作时出现错误。

问题:

您正试图访问您的怪物,如下所示:

selectedMonster.GetComponent()

这意味着如果你的场景中有多个怪物类型的游戏对象,Unity将无法确定你指的是哪一个

当你实例化一个怪物时,你只需要

Instantiate(selectedMonster.GetComponent<Monsters>().Monster, new Vector3(Random.Range(1,8), Random.Range(1,8), -1), Quaternion.identity);
然后进行迭代

foreach (Monster thisMonster in monsters){
   //check things here
}
另一个选择是在实例化怪物时将其存储在数组中

//Define as global variable a list of Monsters
List<GameObject> monsterList = new List<GameObject>();

//Then you instantiate then like
monsterList .add((GameObject)Instantiate(selectedMonster.GetComponent<Monsters>().Monster, new Vector3(Random.Range(1,8), Random.Range(1,8), -1), Quaternion.identity));

这样你就可以摧毁任何接触到你“单位”的怪物,你就不会有参考问题。

问题:

您正试图访问您的怪物,如下所示:

selectedMonster.GetComponent()

这意味着如果你的场景中有多个怪物类型的游戏对象,Unity将无法确定你指的是哪一个

当你实例化一个怪物时,你只需要

Instantiate(selectedMonster.GetComponent<Monsters>().Monster, new Vector3(Random.Range(1,8), Random.Range(1,8), -1), Quaternion.identity);
然后进行迭代

foreach (Monster thisMonster in monsters){
   //check things here
}
另一个选择是在实例化怪物时将其存储在数组中

//Define as global variable a list of Monsters
List<GameObject> monsterList = new List<GameObject>();

//Then you instantiate then like
monsterList .add((GameObject)Instantiate(selectedMonster.GetComponent<Monsters>().Monster, new Vector3(Random.Range(1,8), Random.Range(1,8), -1), Quaternion.identity));

这样你就可以摧毁任何接触到你的“单位”的怪物,你就不会有参考问题。

请不要发布你的代码截图。改为发布代码。请不要发布代码的屏幕截图。改为张贴代码。