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 SnStarr · Jan 21, 2015 at 07:45 AM · destroy-clonescurrency

How to determine which GameObject to destroy?

I have this script here that will award gold to the player if the game object(AIPlayer) "dies" (if his health

 void Awake () 
 {
     currency = GameObject.FindGameObjectWithTag("GameManager").GetComponent<Currency>();
     player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();
 }

 void Update () 
 {
     if(player.HP <= 0)
     {
         Destroy(this.gameObject);
         currency.CalculateGold(25);
     }
     else if(player.HP > 0)
     {

     }
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 AndresBarrera · Jan 21, 2015 at 08:41 AM 0
Share

It is not clear what are you doing. Is that script attached to your player? or to each enemy? If it is attached to each enemy, aside from the AIPlayer script, then you can try:

 player = GetComponent<AIPlayer>();//Find the AIPlayer attached to this same GameObject
 
 //If you use FindGameObjectWithTag("AIPlayer") it will search all the scene and return only one object, but you said you have several enemies





avatar image SnStarr · Jan 21, 2015 at 09:35 AM 0
Share

Yes this script along with the AIPlayer script are attached to the enemy. Four enemies are instantiated at runtime from a prefab that is Tagged with AIPlayer. So when it says to Destroy this.gameobject it destroys all clones of the prefab in the scene with that tag? I am sorry I cannot explain better. Here is a quick video of the problem on dropbox. https://www.dropbox.com/s/f41jq325wmo6fu9/bandicam%202015-01-21%2003-23-57-255.avi?dl=0You will see once the player takes his turns and then the AI takes their turns that upon killing the instantiated AI Player, all instantiated AI Players are destroyed and the player is awarded gold for all 4 kills even though only one was actually killed, they were all destroyed.

2 Replies

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

Answer by Superrodan · Jan 21, 2015 at 09:40 AM

Does it work if you change this line:

 player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();

to

 player = this.gameObject.GetComponent<AIPlayer>();

Because this script is on every enemy, I believe it should be able to find the component of itself rather than finding the component using the tags.

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 SnStarr · Jan 21, 2015 at 09:43 AM 0
Share

Wow. That does fix it. Idk why I couldn't see that, guess it just needed fresh eyes. Thanks alot.

avatar image Superrodan · Jan 21, 2015 at 09:43 AM 0
Share

Also, sometimes the problem is something different than it seems. I would double check to be sure that whatever is causing the enemies to take damage is only affecting one of the enemies at once. It could be that all of the enemies are taking damage when one gets hit.

If that were happening then they would all reach 0 HP at the same time and die.

avatar image SnStarr · Jan 21, 2015 at 09:45 AM 0
Share

Yeah I did already check to see if they were taking damage at the same time, and they were not. It seems that it was just because they shared a tag.

avatar image
1

Answer by AndresBarrera · Jan 21, 2015 at 09:45 AM

The problem is that FindGameObjectWithTag will look for an object in all your scene, so your enemies may end referencing other enemies through the 'player' variable. Since both scripts (AIPlayer and this one) are attached to the same GameObject, you only need to use a GetComponent to find the specific AIPlayer for the object.

 player = GetComponent<AIPlayer>();
Comment
Add comment · Show 1 · 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 SnStarr · Jan 21, 2015 at 09:48 AM 0
Share

Thanks everybody.

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

27 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

Related Questions

How to destroy bullets? 2 Answers

How do I put an image inside text? 2 Answers

Script won't destroy prefab clones when overlapping... plz help 1 Answer

Advice on how to restore in game purchases bought with soft currency (earned) not just hard currency (real money)? 0 Answers

Destroying Children Individually (Horrible..) 2 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