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;
}
}
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.
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.
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.
Your answer
Follow this Question
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