Why I'm getting all the time MissingReferenceException ?

The full exception message :

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.
GenerateWalls+d__12.MoveNext () (at Assets/Scripts/GenerateWalls.cs:84)

Line 84 is :

objectToScale.transform.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));

objectToScale is not null but I’m getting the exception on this line.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateWalls : MonoBehaviour
{
    public GameObject gameObjectToRaise;
    public float duration;
    public Vector3 raiseAmount;
    public bool go = false;
    public Color[] colors = new Color[4];
    public bool randomColors = false;

    private GameObject objtoraise;
    private GameObject[] walls;
    private bool scaleOver = false;

    private void Start()
    {
        Init();

        ColorWalls();

        // The z Axis must be minimum 1 or any value above 0 could be also 0.1f
        // but it's better to keep it minimum as 1 by default.
        if (raiseAmount.z < 1)
        {
            raiseAmount.z = 1f;
        }

        if (go)
        {
            StartCoroutine(ScaleOverSeconds(objtoraise, new Vector3(raiseAmount.x, raiseAmount.y,
                raiseAmount.z), duration));
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            //if (scaleOver)
            //{
            if (objtoraise != null)
            {
                if (raiseAmount.z < 1)
                {
                    raiseAmount.z = 1f;
                }

                StopCoroutine("ScaleOverSeconds");

                Destroy(objtoraise);

                Init();

                ColorWalls();

                StartCoroutine(ScaleOverSeconds(objtoraise, new Vector3(raiseAmount.x, raiseAmount.y,
                    raiseAmount.z), duration));

                scaleOver = false;
                //}
            }
        }
    }

    private void Init()
    {
        objtoraise = Instantiate(gameObjectToRaise);
        objtoraise.name = "Walls";

        walls = GameObject.FindGameObjectsWithTag("Wall");
    }

    public IEnumerator ScaleOverSeconds(GameObject objectToScale, Vector3 scaleTo, float seconds)
    {
        if (objectToScale != null)
        {
            float elapsedTime = 0;
            Vector3 startingScale = objectToScale.transform.localScale;
            while (elapsedTime < seconds)
            {
                objectToScale.transform.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));
                elapsedTime += Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }

            objectToScale.transform.localScale = scaleTo;

            scaleOver = true;
        }
    }

    private void ColorWalls()
    {
        for (int i = 0; i < walls.Length; i++)
        {
            if (randomColors)
            {
                walls[i].transform.GetChild(0).GetComponent<Renderer>().material.color
                    = GetRandomColour32();
            }
            else
            {
                walls[i].transform.GetChild(0).GetComponent<Renderer>().material.color = colors[i];
            }
        }
    }

    private Color32 GetRandomColour32()
    {
        //using Color32
        return new Color32(
          (byte)UnityEngine.Random.Range(0, 255), //Red
          (byte)UnityEngine.Random.Range(0, 255), //Green
          (byte)UnityEngine.Random.Range(0, 255), //Blue
          255 //Alpha (transparency)
        );
    }
}

The solution if I’m not wrong is to check for null in the while loop.

public IEnumerator ScaleOverSeconds(GameObject objectToScale, Vector3 scaleTo, float seconds)
    {
        if (objectToScale != null)
        {
            float elapsedTime = 0;
            Vector3 startingScale = objectToScale.transform.localScale;
            while (elapsedTime < seconds)
            {
                if (objectToScale == null)
                {
                    yield return null;
                }
                else
                {
                    objectToScale.transform.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));
                    elapsedTime += Time.deltaTime;
                    yield return new WaitForEndOfFrame();
                }
            }

            objectToScale.transform.localScale = scaleTo;

            scaleOver = true;
        }
    }

While that might circumvent the problem, it isn’t solving the problem. You were already checking if “objectToScale” was null at the beginning of that IEnumerator. Your “solution” causes the IEnumerator to just be called repeatedly forever. “yield return null” and “yield return new WaitForEndOfFrame” are functionally equivalent. Sure, it does nothing, but having it unintentionally run in the background isn’t exactly comforting either.

If “objectToScale” was indeed null, the error should be a Null reference exception, rather than saying the gameobject has been destroyed.

In the code snipplet, on line 54, you destroy the object in the midst of starting the coroutine twice. I’ve no idea what logic you are trying to do, but that should be the source of the problem.