Instantiated GameObject not assigning tag

Hi Guys,

The title said it. I’m instantiating a gameobject at runtime using the Instantiate function. I’m trying to give it a tag at runtime, but it doesn’t like it… I’ll show you!!

OBJECT INSTANTIATION

	public void Smack(int whichPaddle)
	{
		
		if(whichPaddle == 1)
		{
			paddleTransform = new Vector3(0, 20, 0);
			
			GameObject newPaddle = (GameObject)Instantiate(paddleOnePrefab, (transform.position - paddleTransform), Quaternion.identity);
			
			Debug.Log("Instantiated paddle");
			
			if(this.tag == "PlayerOne")
			{
				Debug.Log("Tagged One paddle");
				newPaddle.transform.tag = "PaddleOne";
				newPaddle.tag = "PaddleOne";
				Debug.Log("newPaddle tag = "+ newPaddle.tag);
			}
			if(this.tag == "PlayerTwo")
			{
				Debug.Log("Tagged Two paddle");
				newPaddle.transform.tag = "PaddleTwo";
				newPaddle.tag = "PaddleTwo";
				Debug.Log("newPaddle tag = "+ newPaddle.tag);
			}
			
			StartCoroutine( AnimTime(newPaddle) );
		}
		
		if(whichPaddle == 2)
		{
			paddleTransform = new Vector3(0, 40, 0);
			
			GameObject newPaddle = (GameObject)Instantiate(paddleTwoPrefab, (transform.position + paddleTransform), Quaternion.identity);
			Debug.Log("Instantiated paddle");
			
			if(this.tag == "PlayerOne")
			{
				Debug.Log("Tagged One paddle");
				newPaddle.transform.tag = "PaddleOne";
				newPaddle.tag = "PaddleOne";
				Debug.Log("newPaddle tag = "+ newPaddle.tag);
			}
			if(this.tag == "PlayerTwo")
			{
				Debug.Log("Tagged Two paddle");
				newPaddle.transform.tag = "PaddleTwo";
				newPaddle.tag = "PaddleTwo";
				Debug.Log("newPaddle tag = "+ newPaddle.tag);
			}
			
			StartCoroutine( AnimTime(newPaddle) );
		}
	}

ONCOLLISIONENTER ON ANOTHER CLASS

	IEnumerator OnCollisionEnter(Collision collision)
	{
		StopCoroutine( "TargetColony" );
		StopCoroutine( "ETATarget");
		
		Debug.Log("Beginning Coll Switch"+target);
		Debug.Log("Beginning Coll Switch - coll.tag = "+collision.collider.gameObject.tag);
		
		int collToDmg = (int)(collision.relativeVelocity.magnitude * 0.2f);
		
		if(collision.collider.gameObject == playerOne || collision.collider.gameObject.tag == "PaddleOne")
		{
			if(collision.collider.gameObject == playerOne)
			{
				collision.collider.GetComponent<PlayerScript>().LoseHealth(collToDmg);
			}
			
			Debug.Log("Coll Switch Called");
			target = playerTwo;
		}
		
		if(collision.collider.gameObject == playerTwo || collision.collider.gameObject.tag == "PaddleTwo")
		{
			if(collision.collider.gameObject == playerTwo)
			{
				collision.collider.GetComponent<PlayerScript>().LoseHealth(collToDmg);
			}
			
			Debug.Log("Pre Coll Switch Called"+target);
			target = playerOne;
			Debug.Log("Post Coll Switch Called"+target);
		}

ACTUAL LOGS

Instantiated paddle
UnityEngine.Debug:Log(Object)
AbilityPaddle:Smack(Int32) (at Assets/Script/AbilityPaddle.cs:23)
AI:Update() (at Assets/Script/AI.cs:29)

Tagged Two paddle
UnityEngine.Debug:Log(Object)
AbilityPaddle:Smack(Int32) (at Assets/Script/AbilityPaddle.cs:34)
AI:Update() (at Assets/Script/AI.cs:29)

newPaddle tag = PaddleTwo
UnityEngine.Debug:Log(Object)
AbilityPaddle:Smack(Int32) (at Assets/Script/AbilityPaddle.cs:37)
AI:Update() (at Assets/Script/AI.cs:29)

Beginning Coll SwitchPlayerTwo (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
<OnCollisionEnter>c__Iterator6:MoveNext() (at Assets/Script/MoonBehaviour.cs:64)

Beginning Coll Switch - coll.tag = Untagged
UnityEngine.Debug:Log(Object)
<OnCollisionEnter>c__Iterator6:MoveNext() (at Assets/Script/MoonBehaviour.cs:65)

Beginning Coll SwitchPlayerTwo (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
<OnCollisionEnter>c__Iterator6:MoveNext() (at Assets/Script/MoonBehaviour.cs:64)

Beginning Coll Switch - coll.tag = Untagged
UnityEngine.Debug:Log(Object)
<OnCollisionEnter>c__Iterator6:MoveNext() (at Assets/Script/MoonBehaviour.cs:65)

So the problems self explanatory really. In the class I instantiate it recognises I gave it a tag… and when I press pause in the editor and look at the gameobject it shows up as the correct tag… but in collision it has no tag apparently???

Can you help!

First you need to add the tag in the tag list , if PlayerOne is not in the tag list then it want work

use .name instead.

There’s no need to use tag. Tags are stupid tbh. Having to be pre-defined… I mean really…

Okay I’ve now found a problem with using .name.

On collision it doesn’t reference the name of the main gameobject. Instead it’s referencing the component it’s hit… So ‘cylinder’, or ‘cube’. Not the actual name of ‘PlayerOne’ or anything like that. I don’t want to change the name of every component hittable within a main GameObject. How is this handled as standard within Unity?

Failing that why is ‘tag’ not recognised???

use transform.root.name if you only want to deal with the top-level gameobject.

tag will work, but as mentioned, it has to be defined in the list pre-defined tags.