- Home /
can you have a Tag inside a tag and check for collision with that?
currently i have a Player (moves and stuff) (tagged Player)within the Player i have a sword which (tagged sword). i want to check if a cube hits the sword. how would i go about this?
i have tried using triggers and normal collision (have used rigidbody, box, spehere,mesh...collders)
this script is attached to a cube
function OnCollisionEnter(hit : Collision) {
if (hit.gameObject.tag=="sword"){ print ("hit"); }}
Why don't you try it first before you post a question? Yes, it's a workable solution.
It's probably easier to add a script to the player keeping track of all the data. Like weapon: sword; health: x; sta$$anonymous$$a: y; etc..
Answer by Bampf · May 06, 2011 at 01:42 PM
You didn't say what exactly is happening, but odds are you are having the common problem of not getting notified of any collisions. If you search Answers for OnCollision and OnTrigger you can see how other people have fixed it. Make sure both objects have colliders; one of them needs a Rigidbody on it as well.
It's possible that OnCollisionEnter IS getting called, in which case the problem may be with your If statement. Make sure you spelled and capitalized the tag name correctly in your code. Aldwoni's suggestion can help you see if that's happening: add his Debug.Log command to the first line of your OnCollisionEnter method, or use breakpoints (if you are debugging with MonoDevelop.)
Answer by poncho · May 06, 2011 at 02:09 PM
if you want a tag inside a tag you can use the inside string to determine your inner tag example
your object tag could be "ArmSword"
hit.gameObject.tag.contains("Sword")
the contains will see that your tag have Sword in it so it will be detected as sword, but if you use
hit.gameObject.tag.contains("Arm")
then it will be considered as Arm
is just a vague example but is the easiest way to use your tag inside a tag
Your answer
![](https://koobas.hobune.stream/wayback/20220613132150im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Substitute of collision 2 Answers
Trigger not detecting tag 1 Answer
On Click, destroy object and any colliding object with the same tag. 6 Answers
Collide detection with tag [JS] 0 Answers
Rigidbody Collision detection 1 Answer