So I have a tower surrounded by 4 spawner gameobjects. I want to draw lines to all of them using LineRenderer
. I wrote the following code to achieve this but for some reason I am getting NullReferenceException: Object reference not set to an instance of an object
at the line myLines[i].transform.position = startPos;
public Transform towerPos;
GameObject[] myLines;
void DrawLines()
{
myLines = new GameObject[spawnpoints.Count];
Vector3 startPos = towerPos.transform.position;
for (int i = 0; i < spawnpoints.Count; i++)
{
myLines[i].transform.position = startPos;
myLines[i].AddComponent<LineRenderer>();
LineRenderer lr = myLines[i].GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.SetColors(Color.red, Color.red);
lr.SetWidth(0.3f, 0.3f);
lr.SetPosition(0, startPos);
lr.SetPosition(1, spawnpoints[i].spawnPoint.transform.position);
//GameObject.Destroy(myLine, duration);
}
}
The above function is called at Start(). The tower GameObject is dragged into the towerPos
variable in the editor so dont know why its a null.
Lists
so would not know how to use it in this context. I know that at initialization it will have null values, but am I now populating that array index usingmyLines[i].transform.position = startPos;
? This should overwrite the null, no? \$\endgroup\$ – StuckInPhD Jul 17 '18 at 0:07spawnpoints
in my scene. \$\endgroup\$ – StuckInPhD Jul 17 '18 at 0:13