Reputation: 1723
I have a Unity scene with a LineRenderer in it at the top level (ie not child of some component), called "LineOfFlight".
I am trying to get a reference to it programmatically (not via the editor):
LineRenderer
lineOfFlight;
GameObject
tmp = GameObject.Find("LineOfFlight");
if (tmp != null)
lineOfFlight = tmp.GetComponent<LineRenderer>();
Debug.Log("lineOfFlight=" + (lineOfFlight == null ? "null" : "not null") + ", tmp=" + (tmp == null ? "null" : tmp.ToString()));
In the debug log, tmp comes out as 'LineOfFlight (UnityEngine.GameObject)', which looks OK, but lineOfFlight comes out as 'null'. Ie could not get hold of the component.
I am doing all this in Awake(). What have I done wrong?
Upvotes: 0
Views: 432
Reputation: 5173
There is an error somewhere in a part of your code that you have not included in your question, as the code itself is working when formatted properly, and set within functions.
Alternatively your GameObject
called "LineOfFlight" may have a typo in its name, or does not have a LineRenderer
component added to it.
using UnityEngine;
public class LineRendererText : MonoBehaviour
{
LineRenderer lineOfFlight = null;
private void Start()
{
GameObject tmp = GameObject.Find("LineOfFlight");
if (tmp != null)
lineOfFlight = tmp.GetComponent<LineRenderer>();
Debug.Log("lineOfFlight=" + (lineOfFlight == null ? "null" : "not null") + ", tmp=" + (tmp == null ? "null" : tmp.ToString()));
}
}
Upvotes: 0