No Renderer for Screen Space Overlay Game Object (Hide/Unhide)?

Hi, I am new to Unity and am using 2019.4.22f1. I have a simple 3D background scene and am overlaying 2D objects (i.e., HUD like symbols) using Sprite-UI textures (that all works fine). I am simply trying to hide and show various 2D game objects when required based upon incoming network data, but I cannot figure out why the renderer comes back null. Below is the code for this simple object; in the debugger the Renderer rend always comes back null in the Start() function so I cannot hide or unhide the object at runtime in the Update() routine. Everything else in the Update routine works fine. What am I missing? A search suggested using SpriteRenderer instead but that also failed with a GetComponent. A GetCompnent did return something but there was no method to hide/unhide.

The parent to this symbol is a Canvas, i.e.,

HudCanvas/
HudFpmInertial
HudAltitude

Script for the HudFpmInertial game object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HudFpmInertial : MonoBehaviour
{
    public CommsUdp comms;              // UDP Network interface
    public Renderer rend;

    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();   // This always comes back null - why?
    }

    // Update is called once per frame
    void Update()
    {
        if (comms != null )
        {
            if (comms.ownship.sensor.isFpmInertialEnabled)
            {
                // Symbol is active 
                if(rend != null ) rend.enabled = true;   // rend is always null
                const float sf = 15f;
                const float D2R = Mathf.PI / 180f;
                float posx = comms.ownship.sensor.betat * sf;
                float posy = (comms.ownship.sensor.gamma - 
                              comms.ownship.sensor.theta) * sf;
                float rollRadians = comms.ownship.sensor.phi * D2R;
                float acRollSine = Mathf.Sin(rollRadians);
                float acRollCosine = Mathf.Cos(rollRadians);
                float azDeg = (posx * acRollCosine) - (posy * acRollSine);
                float elDeg = (posx * acRollSine) + (posy * acRollCosine);

                transform.localPosition = new Vector2(azDeg, elDeg);
            }
            else
            {
                //  Symbol is not active
                if (rend != null) rend.enabled = false;   // rend is always null
            }
        }
    }
}

Instead of enabling or disabling the renderer (which you don’t use for anything else but showing / hiding, as far as your code shows) you could just get the Image-Component and enable and disable that. It will achieve what you want and is proven to be working. No need to fiddle with the renderer.

1 Like

Thank you FlashMuller! That was a great suggestion, and it worked. I gave up on the renderer method and did this per your suggestion:

public class HudFpmInertial : MonoBehaviour
{
    public CommsUdp comms;                  // UDP Network interface
    private UnityEngine.UI.Image image;     // Texture associated with this object

    // Awake is called when this Unity GameObject is being loaded.
    void Awake()
    {
        image = GetComponent<UnityEngine.UI.Image>();
    }

and in the Update() routine simply used image.enabled and set it true or false as suggested! Brilliant.