Create new transform when other transfrm is destroyes

Hi,

I’m making an AI script for a enemy, it needs to wander end set its own destination inside of a given area.
Now I made this script by following a couple tutorials, but when I destroy the moveSpot.gameobject and want to create a new transform i get the error that its still trying to acces it.

This is my update function:

if (moveSpot.transform == null)
{
//CreateMoveSpot();
moveSpot.localPosition = center + new Vector3(UnityEngine.Random.Range(-size.x / 2, size.x / 2), 0, (UnityEngine.Random.Range(-size.z / 2, size.z / 2)));

        //Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), 1,(Random.Range(-size.z / 2, size.z / 2)));

        moveSpot = Instantiate(moveSpot, moveSpot.localPosition, Quaternion.identity) as Transform;
        moveSpot.name = "MoveSpot";
    }

    else if (moveSpot != null)
    {
        agent.SetDestination(moveSpot.position);
        isWandering = true;

    }

    if (Vector3.Distance(transform.position, moveSpot.position) < 1f)
    {
        Destroy(moveSpot.gameObject);            
    }

I’m a beginner so the code looks a bit weird maybe.

Hope someone can help me!

I was thinking in the wrond way, instead of creating a new movespot I could just change the position(ofcourse …);

this is the code now

       if (moveSpot != null)
    {
        agent.SetDestination(moveSpot.position);

        if (Vector3.Distance(transform.position, moveSpot.position) < 1f)
        {
            moveSpot.localPosition = center + new Vector3(UnityEngine.Random.Range(-size.x / 2, size.x / 2), 0, UnityEngine.Random.Range(-size.z / 2, size.z / 2));

            Debug.Log(moveSpot.position);

        }
    }