Basically, I have a script that goes along the lines of:
public class EdgeCasting : MonoBehaviour {
private LineRenderer edgeRenderer;
private Vector3 _edgeOrigin;
private bool nextNodeCreationInProgress = false;
public Vector3 edgeOrigin {
get {
return _edgeOrigin;
}
set {
_edgeOrigin = value;
edgeRenderer.SetPosition(0, _edgeOrigin);
}
}
void Start() {
edgeRenderer = transform.Find("Edge").GetComponent();
}
...
I noticed that if (from another object) I instantiate the prefab to which this script is attached and then try to set the edgeOrigin property, like so:
newEdge = (EdgeCasting)((GameObject)Instantiate(Resources.Load("Edge"), transform.position, Quaternion.identity)).GetComponent<EdgeCasting>();
newEdge.edgeOrigin = transform.position;
newEdge’s edgeRenderer is not yet initialized / assigned by the time I try to set it indirectly thru the edgeOrigin property.
I assumed Start() was a sort of monobehaviour “constructor” if you will and so it was guaranteed to be called before one was able to manipulate a script instance. I guess I was wrong. So, is there any way to make sure any private members are initialized before being able to start accessing properties in the script / class? I mean other than making the renderer a public property and manually assigning it before using edgeOrigin.
If your LineRenderer is part of your prefab, you can make your edgeRenderer variable public (or add the SerializeField attribute) and link the LineRenderer in the inspector. That way the variable is set automatically when the object is Instantiated.
– Bunny83