gpt4 book ai didi

c# - 在 Unity 中使用 getComponent 从附加到游戏对象的脚本中获取图像

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:52 27 4
gpt4 key购买 nike

我正在尝试引入雷达扫描功能,以便在单击按钮时雷达上的图像会根据对象的标签更新为新图像。我在雷达扫描脚本的 Enemy.GetComponent 行收到“NullReferenceException:对象引用未设置为对象的实例”。但我有图像集?我现在将在下面显示的点 1

enter image description here

我是不是访问错了?这是我的 RadarScan 脚本

public class RadarScan : MonoBehaviour
{
GameObject Enemy;
Image RadarImageToChange;

void Start()
{
Enemy = GameObject.Find("Enemy");
}

public void ChangeImage(Image UpdateImage)
{
if (Enemy.tag == "Enemy")
{
RadarImageToChange = Enemy.GetComponent<MakeRadarObject>().image;
UpdateImage = RadarImageToChange;
}

}
}

这是我的雷达脚本

public class RadarObject
{
public Image icon { get; set; }
public GameObject owner { get; set; }
}

public class Radar : MonoBehaviour {

public Transform playerPos; //position of player
float mapScale = 0.1f; //scale radar size

public static List<RadarObject> radObjects = new List<RadarObject>();

//Registers Object to the radar
public static void RegisterRadarObject(GameObject o, Image i)
{
Image image = Instantiate(i);
radObjects.Add(new RadarObject() { owner = o, icon = image }); //adds to List
}

//It loops through the list looking for the owner existing in the list, when it finds the owner is detroys the icon
public static void RemoveRadarObject(GameObject o)
{
//New list for destroyed objects
List<RadarObject> newList = new List<RadarObject>();
for (int i = 0; i < radObjects.Count; i++)
{
if (radObjects[i].owner == o)
{
Destroy(radObjects[i].icon);
continue;
}
else
newList.Add(radObjects[i]);
}
radObjects.RemoveRange(0, radObjects.Count);
radObjects.AddRange(newList);
}


void DrawRadarDots()
{
//loops through the list and for each Object it gets the owners transform position and determins the difference between it's
//position and the players position, does calculations on the angle and distance and position on a circle using polar equations.
foreach (RadarObject ro in radObjects)
{
Vector3 radarPos = (ro.owner.transform.position - playerPos.position);
float distToObject = Vector3.Distance(playerPos.position, ro.owner.transform.position) * mapScale;
float deltay = Mathf.Atan2(radarPos.x, radarPos.z) * Mathf.Rad2Deg - 270 - playerPos.eulerAngles.y;
radarPos.x = distToObject * Mathf.Cos(deltay * Mathf.Deg2Rad) * -1;
radarPos.z = distToObject * Mathf.Sin(deltay * Mathf.Deg2Rad);

//grabs icon of players objects and make it a child of panel and set it's postion based on radarPos.x and radarPos.z
ro.icon.transform.SetParent(this.transform);
ro.icon.transform.position = new Vector3(radarPos.x, radarPos.z, 0) + this.transform.position;
}
}

//Update is called once per frame
void Update ()
{
DrawRadarDots();
}
}

这是我的堆栈跟踪 enter image description here

最佳答案

but I have a image set

不是真的。您对变量成员资格和/或如何 GetComponent() 有误解有效。

GetComponent()方法(及其变体)用于获取 Component附加到 GameObject 直接。它不会识别/查找组件的成员(包括变量);只有组件本身; 即使这些成员属于 Type Component .

这意味着 null你在 Enemy.GetComponent<Image>();没有被扔在 Image 上引用。正如您所说,那个已经设置好了。


问题出自GetComponent本身:

图像组件未附加到 GameObject ;相反,它的reference 被设置为 MakeRadarObject 中的member 组件。

因此, GetComponent没有找到任何 Image附加到 GameObject 的组件, 所以它返回 null ;正如它应该的那样。

关于c# - 在 Unity 中使用 getComponent 从附加到游戏对象的脚本中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47299398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com