Search Unity

Accessing the 'tag' variable starting as a Collision

Discussion in 'Scripting' started by klindeman, Aug 17, 2005.

  1. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    I am getting a Collision from OnCollisionEnter(), and need to access the tag variable from the Collision it recieves. I currently have OnCollisionEnter(hitMe), and I can access some other stuff, like hitMe.other.gameObject, which I later use to check if its hitting something specific. Problem is, I tried to do hitMe.other.gameObject.tag so that I can edit the tag. How should I be doing this, as it doesn't seem to change the tag.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    hitMe.other.gameObject.tag = "Something";

    should work.
     
  3. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Hmm. Thats what I thought. Doesn't appear to be. Maybe I am setting it BACK to what it was before somewhere else.

    Thanks I shall look around.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Try this:

    hitMe.other.gameObject.tag = "Something";
    print (hitMe.other.gameObject.tag);
     
  5. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    It looks like it prints out the correct tag...

    But it doesn't change in the inspector it seems.

    Here is my code to set the new tag:
    Code (csharp):
    1. function OnCollisionEnter (hitMe)
    2. {    
    3. var rocketArray = GameObject.FindGameObjectsWithTag("Rocket");
    4.    
    5.     for (var object in rocketArray)
    6.     {
    7.         if(hitMe.other.gameObject == object)
    8.         {
    9.             hitMe.other.gameObject.tag = "TankHit";
    10.             Debug.Log(hitMe.other.gameObject.tag);
    11.         }
    12.     }
    13. }
    That Debug.Log prints out TankHit.

    Also, in my AI script, I created this:

    Code (csharp):
    1. var tankHitArray = GameObject.FindGameObjectsWithTag("TankHit");
    2. for (var workdammit in tankHitArray)
    3. {
    4.     Destroy(workdammit);
    5. }
    It doesn't destroy the tank at all...
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Use this instead:

    Debug.Log(hitMe.other.gameObject.tag, hitMe.other.gameObject);

    Then pause the game and select the console entry. This will reveal the hitMe.other.gameObject.
    When you select that, does it not have the tag assigned anymore?

    I tested this a second ago and it seems to work as expected from what i can gather. Maybe another script changes the tag back?
     
  7. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Aha! It seems that I need to have it ignore the other particle child. It was changing IT to the TankHit tag. I was going by your guys example and having two particles under one parent, I liked that effect. Good thinking about seeing which one it was changing the tag of!
     
  8. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Yep. Couple tweaks to my scripts got it working correctly. That is a handy feature I didn't know you could do! (being able to have it point out which one it changed).