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 Commander Quackers · Dec 08, 2013 at 04:09 AM · destroyenemynot workingbullethealth

Enemy health and bullet problem

[Edit]: This script is attached to the enemy, by the way!

Hello, I'm having a little trouble with my enemy health script. I asked a question similar to this one a while ago, but I'm using my newer script now, and can't figure out how to solve this problem.

Here's the health script:

 var hit = 0;
 var bulletHit : AudioSource;
 var bullet : GameObject;
  
 function OnTriggerEnter(){
     hit +=1;
     Update();
     audio.Play();
 }
  
 function Update(){
     if(hit == 3){
         Destroy(bullet.gameObject);
         Destroy(gameObject);
     }
 }

It basically makes it so that if the bullet object enters the trigger on the enemy (Collider set to trigger) it adds a hit to it and if there are 3 hits, it destroys it.

The Problem: The bullets don't get destroyed when they hit the enemy. They continue to travel for 5 seconds (because of the Timed Object Destructor.) I tried to use this, but it didn't work either:

 DestroyImmediate(bullet, true);
     
     and
     
 DestroyImmediate(bullet.gameObject, true);

Can someone help me with this? Thanks.

Comment
Add comment · Show 2
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 Guts · Dec 08, 2013 at 04:31 AM 0
Share

What if you tried destroying the bullet in the OnTriggerEnter function? Isn't that when they hit the enemy?

avatar image Commander Quackers · Dec 08, 2013 at 04:42 AM 0
Share

Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy ins$$anonymous$$d. UnityEngine.Object:DestroyImmediate(Object, Boolean) ThreeHitDeath:OnTriggerEnter() (at Assets/ThreeHitDeath.js:6)

and when I use Destroy:

Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object) ThreeHitDeath:OnTriggerEnter() (at Assets/ThreeHitDeath.js:6)

[Edit] Still trying to fix this, but I keep getting these errors, does anyone know how to fix this?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by santosh810666 · Dec 08, 2013 at 05:54 AM

try this

 var hit = 0;
 var bulletHit : AudioSource;
 var bullet : GameObject;
  
 function OnTriggerEnter(){
     hit +=1;
     Update();
     audio.Play();
 }
 function start(){
      bullet = GameObject.Find("Name of your bullet");
 }
  
 function Update(){
     if(hit == 3){
         Destroy(bullet);
         Destroy(gameObject);
     }
 }
Comment
Add comment · Show 10 · 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 santosh810666 · Dec 08, 2013 at 05:00 AM 0
Share

or else use tags to destroy

avatar image Commander Quackers · Dec 08, 2013 at 06:08 AM 0
Share

Didn't seem to work, I'll try using tags

[Edit] Tags did not work either. I did:

 var hit = 0;
 var bulletHit : AudioSource;
 var bullet : GameObject;
  
 function OnTriggerEnter(){
     hit +=1;
     Update();
     audio.Play();
 }
  
 function Update(){
     if(hit == 3){
         Destroy(gameObject);
         
         Destroy(GameObject.FindWithTag("Bullet"));
     }
 }
avatar image santosh810666 · Dec 08, 2013 at 06:56 AM 0
Share

try this

 var hit = 0;
 var bulletHit : AudioSource;
 var bullet : GameObject;
  
 function OnTriggerEnter(){
     hit +=1;
     Update();
     audio.Play();
 }
 function start(){
      bullet = GameObject.Find("Name of your bullet");
 }
  
 function Update(){
     if(hit.bullet.tag == "Enemy"){
         Destroy(bullet);
         Destroy(gameObject);
       
     }
 
avatar image santosh810666 · Dec 08, 2013 at 07:02 AM 0
Share

in your code your saying that if you hit 3 times to enemy then the bullet should destroy.It is not correct if you want to destroy bullet immediate,say that if your bullet hit enemy then destroy bullet and enemy

avatar image Commander Quackers · Dec 08, 2013 at 07:30 AM 0
Share

$$anonymous$$issingFieldException: System.Int32.bullet

Show more comments
avatar image
0

Answer by Spinnernicholas · Dec 08, 2013 at 07:03 AM

Create a boolean to represent if the bullet is destroyed. Initialize it to false. When the collision occurs, set it to true. Then, in the Bullets Update(), you can call destroy immediate.

Comment
Add comment · Show 3 · 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 Commander Quackers · Dec 08, 2013 at 07:19 AM 0
Share

I haven't used booleans yet, I've researched them a bit, but I don't know how to use them. I've created at the top of my script:

 var isBulletDestroyed : boolean = false;

Now from there on I'm not sure which way to go.

avatar image Spinnernicholas · Dec 10, 2013 at 04:46 PM 0
Share

When bullet is destroyed:

 isBulletDestroyed = true;

then, in Update:

 if(isBulletDestroyed)
 {
   DestoyImmediate(this);
 }

All logical operations(==,!=,<,>,....) return boolean values.

avatar image Commander Quackers · Dec 11, 2013 at 12:16 AM 0
Share

Not working, it seems to think I'm actually trying to destroy the bullet prefab itself, not the bullet that was instantiated when fired. I tried DestroyImmediate and Destroy, they both tell me to try the opposite of each other.

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

19 People are following this question.

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

Related Questions

Enemy Health Problems 1 Answer

destroy a non-trigger object hitting a trigger object? 1 Answer

How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript) 2 Answers

health code help 1 Answer

Enemy health bar over them and destroy actor 0 Answers


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