How do I dynamically modify tags based on a random selection made in another script?

So I’m a novice, stupid and struggling to figure out why my game isn’t behaving.


I’m trying to make a bubble/balloon popper style game and have a Spawner script (Spawner.cs) which holds a pool of objects, this script then selects, at random, one of the objects that’s in the pool. From here another script, which is attached to all of the objects in the pool (Bubble.cs) is supposed to check the Spawner script to see which object is currently selected (safeBubble) and then if this game object matches it should tag it as “Safe”. It does check and set initially but then they don’t de-set/ re-check after that first safeBubble is chosen at the beginning of the game, like it should. The idea being that the safeBubble changes at random intervals and thus the tags on the active bubble gameobject need to change to reflect whether they match said safeBubble.

Code & What I’ve tried##

Here’s my scripts, I though that a simple if statement to set and check the tag in both the Start and Update functions of the Bubble.cs would work but it doesn’t and I’m just not figuring it out .
I’ve tried using a switch, but it has the same result.


public class Bubble : MonoBehaviour{

  public Spawner spawner;

  public float popPoint = 10f;
  public float spinSpeed;
	public float riseSpeed;

  public AudioSource popAudio;

  public bool safe;

    void Start(){ ="(Clone)","").Trim();
      riseSpeed= Random.Range(0.05f, 0.1f);
      spinSpeed= Random.Range(0.1f, 0.5f);

      if (this.gameObject == spawner.safeBubble) {
        this.tag ="Safe";
      } else{
        this.tag ="Bad";

    void Update(){
      if ( this.gameObject == spawner.safeBubble ){
        this.tag ="Safe";
      } else {
        this.tag ="Bad"; 

      transform.Translate (Vector3.up * riseSpeed, Space.World);
  		transform.Rotate (Vector3.right * 2* Time.deltaTime);
  		transform.Rotate (Vector3.up * spinSpeed* Time.deltaTime);

  		if(gameObject.transform.position.y >= popPoint ){

    void Popped(){
      // play pop audio
      popAudio.Play ();
      Debug.Log ("I have been popped");



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Spawner : MonoBehaviour{
  public int index;
  public int randomIndex;

  public List<GameObject>[] pool;
  public GameObject[] bubbles;

  public GameObject greenBubble;
  public GameObject orangeBubble;
  public GameObject pinkBubble;
  public GameObject purpleBubble;
  public GameObject redBubble;
  public GameObject blueBubble;

  public GameObject safeBubble;

  public float timeBetweenSpawns;
  public float speed = 1.0f;

	public float swapTime;
	public Transform spawnLoc;

	public Vector3 Position1;
	public Vector3 Position2;

  public Image BubbleToPopImg;
	public Sprite green;
	public Sprite orange;
	public Sprite pink;
	public Sprite purple;
	public Sprite red;
	public Sprite blue;

  public bool willGrow = true;

  void Awake(){
		bubbles = new GameObject[6];
		bubbles [0] = greenBubble;
		bubbles [1] = orangeBubble;
		bubbles [2] = pinkBubble;
		bubbles [3] = purpleBubble;
		bubbles [4] = redBubble;
		bubbles [5] = blueBubble;

		safeBubble = bubbles[Random.Range(0, 5)];

		swapTime = Random.Range (2f,3f);
		// Randomiser ();

		timeBetweenSpawns = Random.Range (0.6f,1.2f);

		InvokeRepeating ("Spawn", 1f, timeBetweenSpawns);

		pool = new List<GameObject>[bubbles.Length];
		for (int i = 0; i < bubbles.Length; i++){
			pool *= new List<GameObject>();*
  •  }*

// Debug.Log("Safe Bubble is " + safeBubble);

  • }*

public GameObject GetPooledObject(){

  •  randomIndex = Random.Range(0, pool.Length);*
  •  for (int i = 0; i < pool[randomIndex].Count; i++){*

_ GameObject go = pool[randomIndex];_
* if (!go.activeInHierarchy){*
* return go;*
* }*
* }*

* if (willGrow){*
* GameObject obj = (GameObject)Instantiate(bubbles[randomIndex]);*
* pool[randomIndex].Add(obj);*
* return obj;*
* }*
* return null;*
* }*

public void Spawn(){
* GameObject bubbles = GetPooledObject ();*
* if(bubbles != null){*
* bubbles.transform.position = spawnLoc.transform.position;*
* bubbles.transform.rotation = spawnLoc.transform.rotation;*
* bubbles.SetActive(true);*
* }*

* }*

void Update(){
_ transform.position = Vector3.Lerp (Position1, Position2, Mathf.PingPong(Time.time*speed, 1.0f));_
timeBetweenSpawns -= Time.deltaTime;
swapTime -= Time.deltaTime;

* if(timeBetweenSpawns<=0){*
* timeBetweenSpawns = Random.Range (0.6f,1.1f);*
* }*

* if(swapTime <= 0){*
* Randomiser ();*
* swapTime = Random.Range (3f,6f);*
* }*

case 5: BubbleToPopImg.sprite = blue; break;
case 4: BubbleToPopImg.sprite = red; break;
case 3: BubbleToPopImg.sprite = purple; break;
case 2: BubbleToPopImg.sprite = pink; break;
case 1: BubbleToPopImg.sprite = orange; break;
case 0: BubbleToPopImg.sprite = green; break;

* }*

public void Randomiser(){
* index = randomIndex;*
* safeBubble = bubbles[index];*
// Debug.Log("Safe Bubble is " + safeBubble);
* }*


Any Help is greatly appreciated.
Thanks in advance.

For anyone interested this issue was solved.

I wasn’t actually comparing the name of the object I had selected as the safeBubble, I was comparing the object itself.

I used

 this.gameObject == spawner.safeBubble 

When I should have used ==

or even

this.gameObject.ToString() == spawner.safeBubble.ToString()

Hope this helps someone in the future.