Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# 在对象向量中引用对象 我需要创建一个向量或对象列表(以允许我这样做的为准) 实例化一个对象并将其添加到向量或对象列表中 我需要引用对象向量(或对象列表)中的对象,并引用该对象的组件来初始化存储在游戏管理器中的变量_C#_Object_Unity3d_Vector - Fatal编程技术网

C# 在对象向量中引用对象 我需要创建一个向量或对象列表(以允许我这样做的为准) 实例化一个对象并将其添加到向量或对象列表中 我需要引用对象向量(或对象列表)中的对象,并引用该对象的组件来初始化存储在游戏管理器中的变量

C# 在对象向量中引用对象 我需要创建一个向量或对象列表(以允许我这样做的为准) 实例化一个对象并将其添加到向量或对象列表中 我需要引用对象向量(或对象列表)中的对象,并引用该对象的组件来初始化存储在游戏管理器中的变量,c#,object,unity3d,vector,C#,Object,Unity3d,Vector,注意:这不是家庭作业或与工作相关的作业。这是我正在研究的一个个人游戏机制。我可以完成一个和两个。三是我被困的地方。我不确定是使用向量还是列表,也不知道如何在Unity C#(使用MonoDevelop)(单例模式)中引用向量或列表中的特定对象 instance=实例化(预置、spawnPoint.position、spawnPoint.rotation); instance.GetComponent().enabled=false; cameraTwo=instance.GetComponent

注意:这不是家庭作业或与工作相关的作业。这是我正在研究的一个个人游戏机制。我可以完成一个和两个。三是我被困的地方。我不确定是使用向量还是列表,也不知道如何在Unity C#(使用MonoDevelop)(单例模式)中引用向量或列表中的特定对象

instance=实例化(预置、spawnPoint.position、spawnPoint.rotation);
instance.GetComponent().enabled=false;
cameraTwo=instance.GetComponent();

cameraTwo变量需要设置为向量或列表中对象的组件

我列表中的项目包含我需要访问的摄影机组件。问题是必须指向特定对象才能获取其组件

using System.Linq;
...
List<GameObject> myList = new List<GameObject>();
public GameObject GetGameObjectWithCameraComponent() {
    return myList.FirstOrDefault(item => item.GetComponent<Camera>() != null);
}

public IEnumerable<GameObject> GetAllGameObjectsWithCameraComponent() {
    return myList.Where(item => item.GetComponent<Camera>() != null);
}
您可以使用System.Linq查找包含摄影机组件的项目

using System.Linq;
...
List<GameObject> myList = new List<GameObject>();
public GameObject GetGameObjectWithCameraComponent() {
    return myList.FirstOrDefault(item => item.GetComponent<Camera>() != null);
}

public IEnumerable<GameObject> GetAllGameObjectsWithCameraComponent() {
    return myList.Where(item => item.GetComponent<Camera>() != null);
}
使用System.Linq;
...
List myList=新列表();
公共游戏对象GetGameObjectWithCameraComponent(){
返回myList.FirstOrDefault(item=>item.GetComponent()!=null);
}
public IEnumerable GetAllGameObjectsWithCameraComponent(){
返回myList.Where(item=>item.GetComponent()!=null);
}
列表[0].GetComponent();
问题解决了。感谢您的回复。

欢迎来到Stack Overflow!请确保您已经阅读了主题。特别是,在您的情况下,我们需要一个;否则,您的问题将很快脱离主题或过于宽泛。“这就是我被卡住的地方”请显示您的code.instance=Instantiate(预置,spawnPoint.position,spawnPoint.rotation);instance.GetComponent().enabled=false;cameraTwo=instance.GetComponent();cameraTwo变量需要设置为向量或列表中对象的组件。列表中的项目类型是什么?通过“cameraTwo variable need to the component of a object”,您的意思是列表中的项目具有“Camera”类型的成员,还是需要向列表中的游戏对象动态添加新组件(使用GameObject.AddComponent),别忘了检查list.Length或list.Count以避免NPE。