0
\$\begingroup\$
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.

\$\endgroup\$
1
\$\begingroup\$

The code is still keeping a reference to the destroyed object in the instancesToMove list, even though it is "destroyed". As such, it hasn't been garbage collected yet.

Unity overrides the GameObject == comparison to return true when compared to null if the object has been destroyed. So the following should stop the error you are getting.

for (int i = 0; i < allLines.Length; i++)
{
    if (instancesToMove[i] != null && instancesToMove[i].transform.position == endPos)
    {
        DestroyImmediate(instancesToMove[i]);
        DestroyImmediate(allLines[i]);
    }
}

You can also use something like the following once each update to remove all destroyed objects from the list;

instancesToMove.RemoveAll(x => x == null); 

Alternatively, you can wait until all lines have moved before destroying them (like you are asking about) with something like;

bool finished = true; 
for (int i = 0; i < allLines.Length; i++)
{
    if (instancesToMove[i].transform.position != endPos)
    {
        finished = false; 
        break; 
    }
}
if (finished)
{
    //insert destruction code here. 
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.