How can i compare Text string from multiple child Text objects?

Hello everyone, let me start explaining in detail the issue.


I got a GameObject which has 6 child objects, each child object has a grandchild Text. Gameobject>Image>Text


The Image child has 2 colliders Box which is always active and Polygon which functions as a trigger and is deactivated. **Note: both colliders are active in picture. One of them has a diamond shape, ignore that please.**

The Image object has 2 main scripts, RandNumber.cs assigns a random number from 0 - 9 to the Text object, the second script BoxInteractions.cs deactivates a box if hit and currently prints in console the name of the boxes neighbor to the box hit and its Number. That’s the reason for the colliders overlapping with each other.


RandNumber.cs:

public class RandNumber : MonoBehaviour {

	public static RandNumber instance;
	public Text number;
	public float digit;

	// Use this for initialization
	void Start () {

		digit = Random.Range(0, 9);
		digit = Mathf.Round(digit);

		number.text = "" + digit;

	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		other.GetComponent<Text>();

	}

}

BoxInteractions.cs

public void Die()
	{
		//Destroy(gameObject);
		gameObject.SetActive(false);
	}

	public void beenHit()
	{
		Debug.ClearDeveloperConsole();
		Collider2D cross = GetComponent<PolygonCollider2D>();
		cross.enabled = true;
		Text number = GetComponentInChildren<Text>();
		Collider2D[] neighbors = new Collider2D[4];
		int ey = Physics2D.OverlapCollider(cross, new ContactFilter2D(), neighbors);

		foreach (Collider2D col in neighbors)
		{
			if (col != null)
				Debug.Log(number.text);
		}

		foreach (Collider2D col in neighbors)
		{
			if (col != null)
				Debug.Log(col.name);
		}

		Die();
	}

Cannon.cs, this is the part responsible for shooting/hitting the boxes.

void Shoot()
	{
		Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
		Vector2 firePointPosition = new Vector2(bullet.position.x, bullet.position.y);
		Vector2 val = mousePosition - firePointPosition;
		RaycastHit2D hit = Physics2D.Raycast(firePointPosition, val, 100, notToHit);
		Effect();
		Debug.DrawLine(firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
		if(hit.collider != null)
		{
			Debug.DrawLine(firePointPosition, hit.point, Color.red);
			//Debug.Log("We hit " + hit.collider.name);
			if(hit.collider.GetComponent<BoxInteraction>() != null)
			{
					hit.collider.GetComponent<BoxInteraction>().beenHit();
			}
		}
	}

The game is somewhat a version of bubble puzzle, a line spawns with random numbers, i get 3 random numbers to choose for "shooting", to explode a box i got to add/subtract/multiply example:

Box(2)Box(4)Box(8)


Cannon (1)(2)(4)


I can choose bullet #2, press add button and shoot Box(2), bullet(2) + Box(2) = 4, the script will compare the result with every box on each side Left/Right/Up which ever is the same explodes with the box that was shoot. Pretty simple right :smiley:



By using Debug.Log i was able to show the Text string in the console, but couldn't show the ones from the boxes that where overlapping, i could only show the name of the object.

Atm the add/subtract/multiply isn’t implemented, what i want to do is add a value to the raycast so i can test if i can do the simple math with the box i hit and destroy(disable) it and the others overlapping.


The way i went by it might not be the best way, or the right way, so i am open to any new ideas and criticism.

P.S I’m new to Unity and C# in general, can defend myself somewhat.

“By using Debug.Log i was able to show the Text string in the console, but couldn’t show the ones from the boxes that where overlapping, i could only show the name of the object.”

 foreach (Collider2D col in neighbors)
         {
             if (col != null)
                 Debug.Log(col.name);
         }

All components, Collider2d, RandNumber (or anything else derived from monobehavior) have a “name” member, which is indeed what you are showing in your loop.
In order to get the “text” value from the RandNumber component (on the same object that has the Collider2d component), one first needs to get the right component, using the GetComponent function, and another member that ALL components have “gameObject”.

RandNumber randNumberComponenent=col.gameObject.GetComponent < RandNumber > ();

Now that you have an object of the correct type, you can get the “text” value from it.

Debug.Log(randNumberComponenent.text);