Tags seem to be lost in instantiated prefabs?

Hi All,

Relative Unity noob, so thanks for your patience.

I’ve got a script attached to a player game object that detects touches, does a raycasthit, detects the tag of the collider and acts appropriately according to the tag.

This script was working absolutely perfectly when the collider was a Game Object with a collider. I’ve taken this Game Object and used to to create a prefab, which is now spawned by spawn points using the spawn script from the 3D platformer tutorial.

The problem is this: when the object is spawned from the prefab, it’s as if my script can’t see the Game Object’s tag. When I check out the actual Game Object at run-time, I can see that it is tagged correctly. However, in my script I have a debug line to log the collider tag to the console, and it always comes back as “Untagged.”

Has anyone else seen this? Anyone know what I’m doing wrong? Might this be a bug?

Thanks again all!

I can’t replicate this in Unity Iphone 1.0.2

Can you post your script - at least the part that detects the tag?

Here’s the code. It’s a bit messy.

I’ve commented out the line

//if (hit.collider.tag == ("Zombie")) {

because I was using it before the currentObject.tag line to read the tag, but neither seems to give me the result I’m after.

function Update()
{
   var hit : RaycastHit;
   
   for (var evt : iPhoneTouch in iPhoneInput.touches)
   {         
      if(evt.phase == iPhoneTouchPhase.Began)
      {
         //if(evt.tapCount == 1)
         //{
               var ray = Camera.main.ScreenPointToRay (evt.position);
               if (Physics.Raycast (ray, hit, 100))
               {
                
                currentObject = hit.collider.gameObject;
				other = currentObject.GetComponent("ZombieWalk");
				Debug.Log(currentObject.tag); 
					if (currentObject.tag == ("Zombie")) {
					//if (hit.collider.tag == ("Zombie")) {
						other.Shot(2);

				   		Debug.Log("Hit Zombie"); 
				   		
					}
					else if (currentObject.tag == ("Head")) {
						other.Shot(6);

				   		Debug.Log("Hit Head");
				   		
					}
					else {
						Debug.Log("Missed");
						}
               }    
            
         //}
      }
   }
}

remove the ( ) around the string

Also you can check the tag at runtime if you test it in the editor by just selecting the instantiated object.

Generally tags are something not optimal on the iphone as they require string operations which count to the slowest operations on the iphone.

The ( ) around the string don’t seem to make any difference.

Is there another way to find a type of gameobject without using tags?

Also, sorry if it wasn’t clear but I was checking the tags in the editor when running, and they appear to be correct.

I’m so dumb!

When I changed the enemy characters to prefabs, I also changed the player to a prefab. When I did it, it enabled a child object that I had previously disabled which had a collider but no renderer. My raycast was hitting this object instead. Noticed it when I looked at the properties of the touch script in the inspector.

Thanks all, and sorry for wasting your time.