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 () {


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

		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"){


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

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) {

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




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:


So your problem comes from this statement:


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