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