- Home /
"Transform" still trying to access after Destroy
Hello everyone, I'm making a game where the character when "Right Clik" aiming to a target to shoot it. But after destroy the gameobject, when I'm aiming again it say "Transform" still trying to access after Destroy. I don't know what to do :( Here's my code part of my code :
#pragma strict
var distance : int;
var lookAtDistance = 10;
var target : Transform;
var damping = 6.0;
var fireSpeed : float = 15;
private var waitTillNextFire : float = 0;
var bullet : GameObject;
var bulletSpawn : GameObject;
var bulletSound : GameObject;
var muzzleFlash : GameObject;
var reloading : boolean = false;
var reloadingAnim : Animation;
var reloadingAnimString : String;
var reloadingSound : AudioSource;
var magSize : int = 15;
var currentMag : int = 15;
var maxExtraAmmo : int = 250;
var currentExtraAmmo : int = 250;
var bulletHudTexture : Texture;
var ammoCountRect : Rect = Rect(25,25,50,25);
var ammoStartX : int = 100;
var ammoY : float = 25;
var ammoSize : Vector2 = Vector2(10,31);
var ammoSpacing : float = 4;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(Input.GetKey(KeyCode.Mouse1) && distance < lookAtDistance)
{
LookAt();
GameObject.Find("player").GetComponent(playerMovement).enabled = false;
}
else
{
GameObject.Find("player").GetComponent(playerMovement).enabled = true;
}
if(!reloading && Input.GetButtonDown("Reload") && currentMag < magSize && currentExtraAmmo > 0)
{
reloading = true;
reloadingAnim.Play(reloadingAnimString);
reloadingSound.Play();
}
if(!reloading && Input.GetButtonDown("Fire1") && currentMag == 0 && currentExtraAmmo > 0)
{
reloading = true;
reloadingAnim.Play(reloadingAnimString);
reloadingSound.Play();
}
if(reloading && !reloadingAnim.IsPlaying(reloadingAnimString))
{
if(currentExtraAmmo >= magSize - currentMag)
{
currentExtraAmmo -= magSize - currentMag;
currentMag = magSize;
}
if(currentExtraAmmo < magSize - currentMag)
{
currentMag += currentExtraAmmo;
currentExtraAmmo = 0;
}
reloading = false;
}
if(currentMag > magSize)
currentMag = magSize;
if(currentExtraAmmo > maxExtraAmmo)
currentExtraAmmo = maxExtraAmmo;
if(currentMag < 0)
currentMag = 0;
if(currentExtraAmmo < 0)
currentExtraAmmo = 0;
var holdSound : GameObject;
var holdMuzzleFlash : GameObject;
if(Input.GetButtonDown("Fire1") && currentMag > 0 && !reloading)
{
if(waitTillNextFire <= 0)
{
currentMag -= 1;
if(bullet)
{
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if(bulletSound)
{
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if(muzzleFlash)
{
holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
waitTillNextFire = 1;
}
}
waitTillNextFire -= Time.deltaTime * fireSpeed;
if(holdSound)
{
holdSound.transform.parent = transform;
}
if(holdMuzzleFlash)
{
holdMuzzleFlash.transform.parent = transform;
}
}
function OnGUI ()
{
for (var i : int = 1; i <= currentMag; i++)
{
GUI.DrawTexture(Rect(ammoStartX + ((i - 1) * (ammoSize.x + ammoSpacing)), ammoY, ammoSize.x, ammoSize.y), bulletHudTexture);
}
GUI.Label(ammoCountRect, currentExtraAmmo.ToString());
}
function LookAt()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
And an another
#pragma strict
var maxDist : float = 1000000000;
var decalHitWallHole : GameObject;
var decalHitWallBlood : GameObject;
var floatInFrontOfWall : float = 0.00001;
var damage : int = 20;
function Update ()
{
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, maxDist))
{
if(decalHitWallHole && hit.transform.tag == "Wall")
{
Instantiate(decalHitWallHole, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
if(decalHitWallBlood && hit.transform.tag == "Enemy")
{
Instantiate(decalHitWallBlood, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
hit.transform.SendMessage("Damage",damage,SendMessageOptions.DontRequireReceiver);
}
}
Destroy(gameObject);
}
Thanks you :)
$$anonymous$$issingReferenceException: 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/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineTransform.cs:27) ai$$anonymous$$g.Update () (at Assets/Script/ai$$anonymous$$g.js:36)
I haven't checked your code, but off the top of my head, I ran into a simular issue, and to solve it I had to do a != null check on the code in question. (In your case this appears to be inside your ai$$anonymous$$g.js file) I assume that's the first script you posted?
So if (target != null) do the aim...
Answer by IvovdMarel · Jun 19, 2014 at 08:01 PM
distance = Vector3.Distance(target.position, transform.position)
add above that
if (transform == null) return;
Edit: Sorry too fast. Just use if (transform != null) and then put the code related to the transform (and the distance) under the if)
thanks a lot ! Now it just tell me the same thing when i'm ai$$anonymous$$g but not after destroy my enemy. I thinks I can figure it out but i'll stay u tuned about it :) Thanks again buddy !
Now, I don't have any error my game keeps running but my character don't move and stay ai$$anonymous$$g after destroy my target :/