Search Unity

Why is the "col" objects tag not changing?

Discussion in 'Scripting' started by wlmr, Apr 1, 2015.

  1. wlmr

    wlmr

    Joined:
    Oct 12, 2014
    Posts:
    11
    Hi!
    I want both the gameobject that the script is attached to to change tag to "0"(upon collision) -- but also the gameobject that it is colliding with, col, to change. Unfortunately col does not change tag. The col object also has this script attached to itself.

    Code (CSharp):
    1.     void OnCollisionEnter2D (Collision2D col) {
    2.         if(col.gameObject.tag[1] == gameObject.tag[1] && gameObject.tag != "0" && gameObject.tag != "4" && col.gameObject.tag[0] != gameObject.tag[0]) {
    3.             gameObject.tag = "0";
    4.             col.gameObject.tag = "0";
    5.             joint.connectedBody = col.gameObject.GetComponent<Rigidbody2D>();
    6.             joint.enabled = true;
    7.             valenceText.text = gameObject.name + System.Environment.NewLine + gameObject.tag;
    8.         }
    9.     }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    your if statement is pretty horrific
    indexing a tag is just picking letters from the tag
    if your tags are just "0", "4", etc then tag[1] will be out of range
    whereas, if your tag is "ball" then tag[1] = the letter 'a'
     
  3. wlmr

    wlmr

    Joined:
    Oct 12, 2014
    Posts:
    11
    I am well aware but that has nothing to do with my question.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Well there's a few things you can do to figure out why it's not working as expected
    Code (CSharp):
    1.         void OnCollisionEnter2D (Collision2D col) {
    2. Debug.Log("Collision happened between " + gameObject.name + " and " + col.gameObject.name);
    3. Debug.Log("Their tags are " + gameObject.tag + " and " + col.gameObject.tag);
    4.        //...
    5.         }
    6.  
    That way you can see what's going on, and then follow your logic through