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 Mystic_Quest · Jan 29, 2017 at 09:15 PM · compareinstances

Compare an instance of an object with another one

Top down rpg, bats and knights for enemies. I want each of them to have different drop tables. I am trying to determine if the current instance of an object (target) is a bat (or a knight). After hours of googling, I can't find a satisfying answer.

  • can't use tags since I already use them to tag all monsters as enemies and objects can only have one tag (why, it is beyond me) and I would like to avoid changing that script if possible.

  • don't want to use child objects with tags to transform them to the parent later for a simple comparison. -Finding with names doesn't work as all instances of the same prefab -All the enemies are of the same type so the code bellow doesn't do anything the drop drops from everyone.

  • googled for a source prefab comparison but it doesn't seem to be possible.

  • tried to get their component (a script) to compare them by them having the same scripts but I get an error.

At the moment the "bat" in update, always finds a bat instance (which I search with name) and i just want to compare it with the specific instance of the monster (could be a knight or bat that dies and drops the item) to ensure that the item will be dropped by a bat.

Any way to determine if my current game object is a Bat?

PS: I could just put different objects as drops in the editor (for example leave the itemdrop1 of the knight's empty and have it only drop for the bat, but this won't work well with large loot tables and different chances and it kind of sloppy.

         if (randomDrop <= 99 && itemDrop1 !=null && target.GetType() == bat.GetType())
         {
             genericDrop1 = Instantiate(itemDrop1, transform.position, Quaternion.identity) as GameObject;
         }
         else return;
     }
 }
   
Comment
Add comment · Show 3
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 Chubzdoomer · Jan 29, 2017 at 09:36 PM 0
Share

Why not create your own "sub-tag" system via something like a public string or even enum?

You could do something like this above your monster class for example:

 public enum $$anonymous$$onsterType
 {
     Bat, $$anonymous$$night
 }

Then inside your monster class, near the top:

     public $$anonymous$$onsterType this$$anonymous$$onsterType;

This will provide you a nice little dropdown menu in the Inspector that allows you to assign each specific monster prefab its respective enum field.

Then wherever you need to check for the type of the monster, you could do something like:

         if (this$$anonymous$$onsterType == $$anonymous$$onsterType.Bat)
             Debug.Log("This is a Bat.");
         else if (this$$anonymous$$onsterType == $$anonymous$$onsterType.$$anonymous$$night)
             Debug.Log("This is a $$anonymous$$night.");

There's probably an even more efficient way of handling it, but that's just one idea that instantly came to $$anonymous$$d.

avatar image Mystic_Quest Chubzdoomer · Jan 29, 2017 at 09:44 PM 0
Share

I found the solution, as usual it was extremely simple after all. I just did this "target.name == "Bat(Clone)"" since all instances have the same name so I can be sure the current instance is a bat or a knight

$$anonymous$$any thanks though for this sub tag trick, I will definitely find a use for it in the future.

avatar image Chubzdoomer Mystic_Quest · Jan 29, 2017 at 09:53 PM 0
Share

I just discovered something really cool that I didn't know about: Sub-tags already exist in Unity!

When you create a tag, all you have to do is type:

 $$anonymous$$ain Tag/Sub Tag

So in your case, you could do something like:

 $$anonymous$$onster/Bat
 $$anonymous$$onster/$$anonymous$$night

Then when you go to select a tag, there will be a sub-menu that appears when you highlight $$anonymous$$onster that contains all of the sub-tags you typed (Bat, $$anonymous$$night, and anything else).

This is probably the easiest and cleanest way of doing it, since there's zero code required to set it up!

EDIT: One downside of this, however, is that the "$$anonymous$$ain Tag" can't really be accessed separately as far as I can tell. If you check for a tag of "$$anonymous$$onster," for example, it will return false. You have to check specifically for "$$anonymous$$onster/Bat" or "$$anonymous$$onster/$$anonymous$$night" in order for it to return true. In that case, I'm thinking enums might still be the best option if you're wanting to use a main tag, but still have a sub tag as well that you can check against.

0 Replies

· Add your reply
  • Sort: 

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

90 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

Related Questions

Why cant I get the object I want (UTS) 0 Answers

Quaternion.RotateTowards stopping precision. 0 Answers

How to create a new instance of an object? 0 Answers

Comparing 2 positions need help 0 Answers

[Beginner] AutoResolve Combat based on Level 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