使用Unity

时间:2017-11-15 04:32:33

标签: c# unity3d

我尝试引入雷达扫描功能,以便在点击按钮时,雷达上的图像会根据带有新图像的对象标记进行更新。我得到了一个" NullReferenceException:对象引用未设置为对象的实例"在雷达扫描脚本中的Enemy.GetComponent行。 但我有一个图像集? 点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

2 个答案:

答案 0 :(得分:3)

  

但我有一个图像集

不是真的。您对变量成员资格和/或GetComponent()的工作方式存在误解。

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

这意味着您null的{​​{1}}未被Enemy.GetComponent<Image>();引用。正如您所说, 设置。

问题来自Image本身:

图片组件未附加到GetComponent;相反,它的 引用 {{1中设置为 成员 } 组件。

因此, GameObject找不到附加到MakeRadarObject的任何GetComponent组件,因此返回Image;应该如此。

答案 1 :(得分:2)

您的案例中的每个Gameobject都不会直接包含Image作为组件,而是MakeRadarObjectRadarScan组件。

Enemy.GetComponent<Image>(); 

返回null,因为Enemy不包含Image

但是,Enemy确实包含MakeRadarObject

RadarImageToChange = Enemy.GetComponent<MakeRadarObject>().variableContainingImage;

此外,您应该检查Enemy是否真的是敌人或其他游戏对象。

尝试添加

if (Enemy.tag == "Enemy") RadarImageToChange = Enemy.GetComponent<Image>();