void Update()
{
if (animateLines)
{
counter++;
for (int i = 0; i < allLines.Length; i++)
{
endPos = allLines[i].GetComponent<EndHolder>().EndVector;
Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
instancesToMove[i].transform.position =
Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
}
for (int i = 0; i < allLines.Length; i++)
{
if (instancesToMove[i].transform.position == endPos)
{
DestroyImmediate(instancesToMove[i]);
DestroyImmediate(allLines[i]);
}
}
}
else
{
counter = 0;
foreach (GameObject thisline in allLines)
{
thisline.GetComponent<LineRenderer>().SetPosition(1, thisline.GetComponent<LineRenderer>().GetPosition(0));
}
}
}
When doing:
for (int i = 0; i < allLines.Length; i++)
{
if (instancesToMove[i].transform.position == endPos)
{
DestroyImmediate(instancesToMove[i]);
DestroyImmediate(allLines[i]);
}
}
It's destroying only some of the lines and instancestomove and then give the exception:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.