Search Unity

Changing an objects tag at Initilisation

Discussion in 'Scripting' started by CoffeeConundrum, Feb 20, 2014.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Hi there.

    I'm currently working on a game that has a set number of hiding places around my scene, all of which have the tag "HidingPlace". At the start of the game, I'm finding all of the game objects with the above tag and keeping them in an array called hidingPlace. From this, I wish to assign the first element of the array the tag of "redGem" to essentially say 'There's something actually hiding in me'

    I've tried to do this by putting the following code into the Start() function of my script.
    Code (csharp):
    1. hidingPlace = GameObject.FindGameObjectsWithTag ("HidingPlace");            //finds all potential hiding places
    2.         hidingPlace [0].tag = "RedGem";                                             //Assigns the red gem to be hidden here.
    3.  
    However from the looks of the debug logs that I've set up, it's assigning the tag RedGem to all of the objects that I've got in my hidingPlace array. Anyone have any idea how I can assign the RedGem tag to just one element in the array. Wishing to have this as a randomly generated event with every new game however thought I would start by assigning it myself for testing purposes - which has proven key!

    Many thanks in advance for any and all answers / suggestions.
     
  2. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    I've tried the same piece of script you posted and for me is working perfectly.

    The only thing I can think about is that you have not defined both the tags: to be able to change a tag, you need to define both HidingPlace and RedGem as available tags.
     
  3. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour {
    4.     void Start () {
    5.         GameObject[] list = GameObject.FindGameObjectsWithTag("HidingPlace");
    6.         if (list.Length != 0) {
    7.             int index = Random.Range(0, list.Length - 1);
    8.             list[index].tag = "RedGem";
    9.             Debug.Log("Index changed = " + index);
    10.         }      
    11.     }
    12. }
    This is the code to assign the second tag to a random tagged GameObject.
    Anyway, I've noticed that the change is applied in a strange way on the objects found: I think is something related to the inner ID of the object.
    If you create a bunch of GameObject and then you will rename some of them in 1, 2, 3, etc. and then you'll apply the tag "HidingPlace" to them, it is not said that if index is 2 you are applying the tag "RedGem" to the GameObject you named as 1 (or 2).
     
  4. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Thanks very much for the response and help! I'll try out the above code tomorrow on my project and see if it works.

    Many thanks again!