Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by StriXBeat · Jun 19, 2014 at 02:45 PM · gameobjecttransformdestroy

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

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Jun 19, 2014 at 03:38 PM 0
Share

Post the errors and their lines please.

avatar image StriXBeat · Jun 19, 2014 at 04:51 PM 0
Share

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

avatar image Rick74 · Jun 19, 2014 at 05:11 PM 0
Share

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...

avatar image StriXBeat · Jun 19, 2014 at 05:26 PM 0
Share

thanks for this answer but it doesn't work either :/

avatar image StriXBeat · Jun 19, 2014 at 05:27 PM 0
Share

and yes it's the first script :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image StriXBeat · Jun 19, 2014 at 09:46 PM 0
Share

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 !

avatar image StriXBeat · Jun 20, 2014 at 12:24 PM 0
Share

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 :/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Change target after destroying it. 2 Answers

How to turn object into a particle system and back? 0 Answers

Can't remove instantiated prefab 0 Answers

How to destroy a transform's parent object. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges