still trying to access a destroyed transform...

hi i’m sorry if its a stupid question but i only asked because i couldn’t solve it.

the error:MissingReferenceException: The object of type ‘Transform’ 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.
UnityEngine.Component.get_transform () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21)
RedSolder.WhereToLook () (at Assets/RedSolder.js:90)
RedSolder.Update () (at Assets/RedSolder.js:26)

i know what the error means i just cant find out what part of the script its coming from, any help is appreciated

#pragma strict
var Health:float=10;
var speed:float=1;
var range:float=5;
private var hit : RaycastHit;

var wheretolook:Transform;

private var lastUpdate:float=0;
private var EnemyBase:GameObject;
private var RotateSpeed:float=10;

function Start () {
	
		EnemyBase=GameObject.FindWithTag("BlueBase");
		wheretolook=EnemyBase.transform;

}

function Update () {
		//defining the closest enemy few times a second
		var ClosestEnemy = Vector3.Distance(FindClosestEnemy().transform.position, transform.position);
		
		//always running these functions so the variables arnt null
		FindClosestEnemy();
		WhereToLook();

		
		Debug.DrawRay(transform.position , transform.forward * 8 , Color.white);
		//this is to avoid colliding and going throigh team mates
		if(Physics.Raycast(transform.position, transform.forward,hit, range)){
				
					//if its a team mate then rotate away
					 if(hit.transform.tag =="Red"){
						RotateAway();
					}

		}
		

		
		//if the enemy is closer then 8
		 if(ClosestEnemy<8) {
		 //and if hes in range then attack
			if(ClosestEnemy<=range){
				Attack();
			}
			//if not in range then move in more
			else{
				transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
			}
			//this makes sure that we have something to look at		
			wheretolook=FindClosestEnemy().transform;
			
			
		}//if the enmey is not closer then 8 then move into their base
		else{
			transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
			wheretolook=EnemyBase.transform;
		}
		//if we have nothing to look at then make the base the primary target
		if(wheretolook ==null){
			wheretolook=EnemyBase.transform;
			transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
		}
		//this is self explanatory
		if(Health<=0){
		Destroy(this.gameObject);
		}
		
}

function RotateAway(){
transform.Rotate(Vector3.up, -170*RotateSpeed* Time.smoothDeltaTime);
}


function WhereToLook(){

var rotate = Quaternion.LookRotation(wheretolook.transform.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime *0.4);


}

function Attack(){

  if(Time.time - lastUpdate >= 1f){
  
		FindClosestEnemy().SendMessageUpwards("ApplyDamage", 1, SendMessageOptions.DontRequireReceiver);
		
		lastUpdate = Time.time;
	}
	
}

//thi is where other enemys call in to give damage
function ApplyDamage (damage : float) {
	Health-=damage;
}
	

        
function FindClosestEnemy () : GameObject {
	
	  // Find all game objects with tag Enemy
	var gos : GameObject[];
	gos = GameObject.FindGameObjectsWithTag("Blue"); 
	
      
        var closest : GameObject; 
        var distance = Mathf.Infinity; 
        var position = transform.position; 
        // Iterate through them and find the closest one
        for (var go : GameObject in gos)  { 
            var diff = (go.transform.position - position);
            var curDistance = diff.sqrMagnitude; 
            if (curDistance < distance) { 
                closest = go; 
                distance = curDistance; 
            } 
        } 
        return closest;    
    }

Have you tried changing

Destroy(this.gameObject);

to

Destroy(gameObject);

This should give you all information you need:

"RedSolder.WhereToLook () (at Assets/RedSolder.js:90) RedSolder.Update () (at Assets/RedSolder.js:26)"

So the exception has been caught in Update at line 26. There you call the function WhereToLook. Furthermore it tells you that the error was inside the function WhereToLook in line 90, however it seems you edited the script because line 90 is outside the function. So i guess the actual line is line 80 because the error comes from:

UnityEngine.Component.get_transform

So your problem comes from this statement:

wheretolook.transform.....

because the object referenced by wheretolook doesn’t exist anymore, therefore you can’t get the transform component from it.

which is the getter of a components transform property