- Home /
Player tag not being detected by Unity?
I have a shooting script that i am using in my game. (Script below) It works by asking if the player is shooting, then asking if the player hit something, and then asking if the player has the tag "Player" or the tag "Vehicle" before destroying the object with that tag.
I have a debug.log setup that shows what the object i am shooting's tag is. Every time it says that the object (player) is untagged even though i created a tag and named it "Player" in unity and tagged the player prefab with it.
Im not super knowledgeable with tags, so all i did was create a new one and name it "Player" Is there something else i need to do? I even tagged the physical model of the character Player. Here is the shooting script and a screenshot of the prefab. *Note, i do not recieve the debug "TagRecieved" or "Destroyed"
function Shoot(){
while(true){
if(shooting == true && weaponnum == 0 && rbullets1 > 0){
rbullets1--;
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
if(hit.transform !=null){
Debug.Log("HitPlayer");
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Debug.Log("tagrecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
}
AudioSource.PlayClipAtPoint(aeksound, transform.position);
}
else if(weaponnum == 0 && rbullets1 == 0){
yield WaitForSeconds(2);
if(rbullets >= 30){
rbullets -= 30;
rbullets1 += 30;
}
else if(rbullets < 30){
rbullets1 += rbullets;
rbullets = 0;
}
}
if(shooting == true && weaponnum == 1 && pbullets1 > 0){
pbullets1--;
AudioSource.PlayClipAtPoint(walthersound, transform.position);
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
if(hit.transform !=null){
Debug.Log("HitPlayer");
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Debug.Log("TagRecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
}
}
else if(weaponnum == 1 && pbullets1 == 0){
yield WaitForSeconds(2);
if(pbullets >= 30){
pbullets -= 30;
pbullets1 += 30;
}
else if(pbullets < 30){
pbullets1 += pbullets;
pbullets = 0;
}
}
if(weaponnum == 0)
yield WaitForSeconds(.075);
else if (weaponnum ==1)
yield WaitForSeconds(.5);
}
}
Player is a built-in tag, you shouldn't recreate it. I just tried on my project and it causes multiple issues. I am not sure why but the new tag doesn't appear in the list, and if I force the game to use it, it will result in an "Untagged" gameobject.
Delete your "Player" tag and use the built-in one.
I made a whole new tag and called it Human, even when i try different tags it wont work. Is there something else that defines a tag? Is my code wrong for detecting tags?
if(shooting == true && weaponnum == 0 && rbullets1 > 0)
{
rbullets1--;
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
//if(hit.transform !=null) // No need, all GO have Transform
//{
Debug.Log("HitPlayer");
Debug.Log(hit.collider.tag); // What is the tag it hits?
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle")
{
Debug.Log("tagrecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
//}
AudioSource.PlayClipAtPoint(aeksound, transform.position);
}
Now it will tell you why the tag is not detected. $$anonymous$$aybe it is hitting something else. Also, As I mentioned, no need to check for Transform, all GameObject have a Transform by default so it cannot be missing since it cannot be removed.
Thank you so much. It turn out that my "bullets" are just hitting the terrain and not the player.
Do you know if this line of code is relative to global or the player? Or even where it would relay from? (im not super knowledgeable with raycasts)
Physics.Raycast(transform.position,transform.forward,hit);
If transform.position refers to the player then it will be ignored:
"Raycasts will not detect colliders for which the raycast origin is inside the collider. "
Answer by Ian-McCleary · Jun 18, 2015 at 11:31 PM
If you want to know what i did, @fafase helped me out to understand what the tag it was actually getting. Turns out my whole raycast was screwed up. @fafase if you change your comment to an answer im happy to make it correct.
I ended up ditching the whole system because it was made poorly.
Answer by devsanunity · Apr 12, 2020 at 12:19 PM
Im facing the same problem. What was the exact solution? @fafase @Ian-McCleary
Your answer
Follow this Question
Related Questions
Destroying GameObject in NetworkBehavor with Javascript 0 Answers
Destroy gameobject by tag (.js) 3 Answers
On Click, destroy object and any colliding object with the same tag. 6 Answers
Character Controller is not interacting with colliders. 1 Answer
how do i create a game object array with gameobjects of multiple tags 1 Answer