Tag doesn't work when gameObject is a child of the Main camera?
I have a gameobject with the tag 'right', and when this gameobject isn't a child of the camera, it gets recognised when I click on it, with this:
if (hitInfo.transform.gameObject.tag == "right")
However, when I make the gameobject a child of the camera (which i need to do) it is no longer recognised. Any ideas on what I can do?
Full code is:
if (Input.GetMouseButtonDown (0)) {
Vector2 pos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (pos), Vector2.zero);
Debug.Log (pos); // space indication
// we have something under our pointer
if (hitInfo) {
if (hitInfo.transform.gameObject.tag == "left") {
playerMovement += Vector2.left * distance;
}
if (hitInfo.transform.gameObject.tag == "right") {
playerMovement += Vector2.right * distance; // or do something else
}
}
Thanks
debug the name of the object on which the raycast is producing the hit. this way you will be able to know if you are getting a right object or not.
It does say the name of the object when it isn't a child of the camera, but when the object is a child of the camera it just says the coordinates that I clicked, not the object name
Answer by UAEo · Mar 30, 2017 at 05:37 PM
Try to make tags with no spaces like "FrontWheels" rather than "Front Wheels"
I was having the same bug, but when I wrote it with no spaces, it magically worked? Try it and tell me plz, so I could tell unity support
Note that Spaced tags does work for gameObject and parent but not with child.
Your answer
Follow this Question
Related Questions
Is there any way how to do raycasts like this? 0 Answers
Strange Raycast problem 0 Answers
Raycast2D Two Objects at the same time 2 Answers
2D Raycast reflection only changes hit.normal 0 Answers
One 2D Raycast Not Stopping Even Though Others Do? 0 Answers