Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DeluxeHD · Apr 19, 2016 at 10:43 PM · editor

Destroying GameObject After Instantiate

So, I have a bit of an issue, I'm currently got this "Spawner" script which spawns Instantiate a prefab every second. Each prefab has a "Health" script.

The problem I am facing is, when the Health is at 0, and I call "Destroy(this.gameObject);" it always destroys the objects in the order that they was spawned in. So if I spawn 5, and I shoot the 5th one till its health is 0, it destroys the 1st one spawned.

If I have to provide the scripts I will do. Thank you if you can help me!

Enemy Health Script - http://pastebin.com/gVj7kcQk

Spawner Script - http://pastebin.com/qMue6Z9C

If this helps. This is the shoot script!

Shoot Script - http://pastebin.com/agLZVrHS

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 Zoogyburger · Apr 19, 2016 at 11:28 PM 0
Share

By all means, provide your scripts!

avatar image DeluxeHD Zoogyburger · Apr 20, 2016 at 12:05 AM 0
Share

I just did. Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zoogyburger · Apr 20, 2016 at 01:24 AM

Try your script like this:

 {
     //Shared Values
     static float health = 100; //Enemys Health
     static public bool dead = false; //Is the Enemy dead?
  
     static public void TakeDamage() //
     {
         health -= 25;
         //Debug.Log("Taken damage");
     }
     void Update()
     {
         if (health <= 0)    // If the enemy health is
         {                   // 0 then it destroys the
             Destroy(gameObject); //Destroys the enemy
         }
         if (health >= 101)  //Makes sure that
         {                   //Health does not
             health = 100;   //go above 100
         }                   //so no problems with op zombies
  
        
     }
  
     void OnTrigger(Collider other) //If it collides with player
     {
         if (other.gameObject.tag == "Player") //if it does collide with the player
         {
             //Debug.Log("Hitting Player"); //Tells you in the console
             Health.Takeaway(); //Takes player health away through other script
         }
     }
 }
Comment
Add comment · Show 7 · 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 DeluxeHD · Apr 20, 2016 at 01:31 AM 0
Share

Unfortunately this doesn't work. Removing the "Health = 100" makes everything just despawn, so it kills everything.

So basically, when I hit the Zombie. It takes the health away on every single cube. Then when the health is 0. It destroys the prefab so everything dies & nothing respawns because the prefab is destroyed.

Thank you for replying though.

avatar image Zoogyburger · Apr 20, 2016 at 01:50 AM 0
Share

Get rid of all static keywords in your scripts.

avatar image DeluxeHD Zoogyburger · Apr 20, 2016 at 01:58 AM 0
Share

I have the static values so other scripts can access them

avatar image Zoogyburger · Apr 20, 2016 at 02:24 AM 0
Share

static - the same variable is shared by ALL instances of the class that are created, and can be private, protected or public. $$anonymous$$eaning all prefabs will be destroyed since all of them are linked by static. You still can access these values without them being static. By

 private EnemyHealth eh;
 
 eh = FindObjectOfType<EnemyHealth> ();

 

and you can use the values by

 eh.health



avatar image DeluxeHD Zoogyburger · Apr 20, 2016 at 02:32 AM 0
Share

No worries, I got it. I did this though ins$$anonymous$$d -

 eh = hit.transform.GetComponent<EnemyHealth>();

but it works!

Thank you. I really do appreciate it!

So do I add this to my "Shoot" script? As Such -

http://pastebin.com/fNeJZFbx

Thanks

avatar image Zoogyburger · Apr 20, 2016 at 02:45 AM 0
Share

You should put:

 eh = FindObjectOfType<EnemyHealth>();

under void Start() do it's not finding the script every update. Other than that you are using it correctly. Just eh.anyVariableInScript should work perfectly fine

avatar image DeluxeHD Zoogyburger · Apr 20, 2016 at 08:34 AM 0
Share

I added it in the update function, so when you fire the raycast and it hits and enemy, it will get the script for that enemy & then I can do things to that enemy? Otherwise wouldn't it just get 1 of the zombies health script until it dies

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

62 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a drop down menu in editor inspector 0 Answers

Expose protected variable in inspector 1 Answer

,Unity Custom Inspector CreateInspectorGUI redraw on change 1 Answer

The variables modified in the inspector are not kept on build 0 Answers

Unity Crashes during package resolution? 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