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!