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 ScottUnityNoob · Feb 02, 2017 at 07:03 PM · ontriggerentertagspickupmultiple objectstagging

How do I pickup multiples of the same item with the same tag?

Hello, I'm very new to C#. I know the answer to this question may be obvious for most, but I'm stuck at the moment.

I wrote a "PlayerHealth/Damage Script". So far, it just has one type of damage, and 4 types of health items. (items are tagged) When I place one of each item in a row, everything works as expected. (Player picks up health OnTriggerEnter, & item destroys itself etc)

The problem is, when I place multiple "copy's" of the Prefab throughout the scene, they basically "don't match up". (ex. entering a trigger of one item (ex. bread) could Destroy a different copy of the same item (ex. bread(2)) and so on.

Here is my Script: using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class PlayerDamageScript : MonoBehaviour {

 int playerHealth = 100;        
 int damage = 10;

 public AudioClip bite;
 public AudioClip drink;
 public AudioClip belch;
 public AudioClip ouch;
 public AudioClip dead;

 void Start () 
 {
     print (playerHealth);
     playerHealth = 100;
 }

 void Update()
 {
     if (playerHealth <= 0) 
     {
         Death ();
     }

     if (playerHealth >= 100)
     {
         playerHealth = 100;
         if (playerHealth > 100) 
         {
             GetComponent<AudioSource> ().clip = belch;
             GetComponent<AudioSource> ().Play ();
         }
     }
 }
     
 //PLAYER HEALTH/DAMAGE TRIGGERS// 
 //*****************************//

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         playerHealth -= damage; 
         print ("Enemy Hit You!" + playerHealth) ;

         GetComponent<AudioSource> ().clip = ouch;
         GetComponent<AudioSource> ().Play ();
     }


     if (other.gameObject.tag == "apple") 
     { 
         playerHealth += 10;
         print ("You ate an Apple + 10 Health =" + playerHealth);
         Destroy (GameObject.FindWithTag ("apple"));                

         GetComponent<AudioSource> ().clip = bite;
         GetComponent<AudioSource> ().Play ();
     }


     if (other.gameObject.tag == ("bread"))
     { 
         playerHealth += 25;
         print ("You ate Bread + 25 Health =" + playerHealth);
         Destroy (GameObject.FindWithTag ("bread"));

         GetComponent<AudioSource> ().clip = bite;
         GetComponent<AudioSource> ().Play ();
     }


     if (other.gameObject.tag == "cheese") 
     { 
         playerHealth += 50;
         print ("You ate Cheese + 50 Health =" + playerHealth);
         Destroy (GameObject.FindWithTag ("cheese"));

         GetComponent<AudioSource> ().clip = bite;
         GetComponent<AudioSource> ().Play ();
     }


     if (other.gameObject.tag == "potion") 
     { 
         playerHealth += 100;
         print ("You drank a healing Potion + 100 Health =" + playerHealth);
         Destroy (GameObject.FindWithTag ("potion"));

         GetComponent<AudioSource> ().clip = drink;
         GetComponent<AudioSource> ().Play ();
     }


 }

 public void Death()
 {
     SceneManager.LoadScene (8);
 }

}

So, how do I make multiple copy's of a prefab and pick up each one (correctly) by tag?

Any help would be greatly appreciated! Thanks!

Comment
Add comment
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

1 Reply

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

Answer by CaioMGA_ · Feb 02, 2017 at 07:13 PM

GameObject.FindGameObjectWithTag() searches the WHOLE scene for a GameObject with a given tag. The first GameObject found is returned.

using other.gameObject you get the GameObject of other

Change the lines 54 , 64, 75, and 86 to be Destroy (other.gameObject);

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 ScottUnityNoob · Feb 02, 2017 at 07:58 PM 0
Share

Awesome, thanks man. I appreciate it!

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

92 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 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 do I affect multiple objects in a single click? 0 Answers

Tag Editor is all grayed out 0 Answers

On collision enter, detect more than one tag? 0 Answers

Can't tag prefab with a string 1 Answer

Screen clear using tags 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