S.Byrne
S.Byrne

Reputation: 75

Get image from script attached to gameobject using getComponent in Unity

I'm trying to introduce a radar scan feature so that when a button is clicked the image on the radar is updated based on the tag of the object with a new image. I'm getting a "NullReferenceException: Object reference not set to an instance of an object" on Enemy.GetComponent line in my Radar Scan Script. but I have a image set? dot 1 which I will now show below

enter image description here

Am I accessing it wrong? Here's my RadarScan script

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;
    }

}
}

Here's my radar Script

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();
    }
 }

Here is my stack trace enter image description here

Upvotes: 2

Views: 7471

Answers (2)

CosmicGiant
CosmicGiant

Reputation: 6439

but I have a image set

Not really. You have a misconception about variable membership and/or how GetComponent() works.

The GetComponent() method (and it's variants) is used to get a Component attached to a GameObject directly. It won't recognize/find members (including variables) of the components; only the components themselves; even if those members are of Type of Component.

This means that the null you're getting at Enemy.GetComponent<Image>(); is not being thrown on the Image reference. As you've said, that one is set.


The problem arises from GetComponent itself:

The image component is not attached to the GameObject; instead, a reference of it is set as a member in the MakeRadarObject component.

As such, GetComponent doesn't find any Image component attached to the GameObject, so it returns null; as it should.

Upvotes: 4

loadingnow
loadingnow

Reputation: 607

Each Gameobject in your case does not directly contain Image as a component, but MakeRadarObject and RadarScan components.

Enemy.GetComponent<Image>(); 

returns null as Enemy does not contain Image.

However, Enemy does contain MakeRadarObject.

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

Besides, you should be checking if Enemy is really an enemy or another gameobject.

Try adding

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

Upvotes: 2

Related Questions