- Home /
Missing Reference Error
Hi I keep getting a missing reference error on my turret script on line 28. Here's 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.Transform.get_position () (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) Turret.Update () (at Assets/Game Stuff/Scripts/Turret.js:28)
Here's the scrip:
var Look : boolean = false;
var Target : Transform;
var damp : float;
var Projectile : Transform;
var Shell : Transform;
var MuzzleFlash : Transform;
var fireRate : float = 5;
var Speed : int = 2000;
var ShellSpeed : int = 500;
var TurretAudio : AudioClip;
private var nextFire = .5;
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Enemy")
{
Look = true;
Target = other.gameObject.transform;
}
}
function Update()
{
if(Look)
{
var rotate = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
Shoot();
}
}
function Shoot()
{
if(Time.time > nextFire)
{
nextFire = Time.time + fireRate;
var muzzleflash = Instantiate(MuzzleFlash, transform.Find("Spawn").transform.position, transform.rotation);
var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, transform.rotation);
projectile.rigidbody.AddForce(transform.forward * Speed);
var shell = Instantiate(Shell, transform.Find("Spawn_2").transform.position, transform.rotation);
shell.rigidbody.AddForce(transform.right * ShellSpeed);
audio.PlayOneShot(TurretAudio);
}
}
function OnTriggerExit(other: Collider)
{
if(other.gameObject.tag == "Enemy")
{
Look = false;
}
}
What I think the problem is, is that when I destroy the target variable it gives me that error. Please help this error does affect my performance. Thanks!
Answer by ThermalFusion · Aug 14, 2012 at 09:17 PM
if(Look && Target != null)
You said it yourself. Make sure you still have a Target, if not, do nothing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075659im_/https://answers.unity.com/themes/thub/images/avi.jpg)