- Home /
The question is answered, right answer was accepted
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 :D
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.
Answer by Glurth · May 10, 2018 at 04:55 PM
"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);
I tried what you said, with a little help from a friend i was able to modified the Foreach loop so i could get the values from the overlapping boxes.
public void beenHit()
{
Debug.ClearDeveloperConsole();
Collider2D cross = GetComponent<PolygonCollider2D>();
cross.enabled = true;
Collider2D[] neighbors = new Collider2D[4];
int ey = Physics2D.OverlapCollider(cross, new ContactFilter2D(), neighbors);
RandNumber randNumberComponent = cross.gameObject.GetComponent<RandNumber>();
foreach (Collider2D col in neighbors)
{
if (col != null)
{
//testing
RandNumber temp = col.gameObject.GetComponentInChildren<RandNumber>();
Debug.Log(temp.digit);
}
}
foreach (Collider2D col in neighbors)
{
if (col != null)
//testing
Debug.Log(col.name);
}
Die();
}
Ah, I didn't realize the RandNumber's were on child-objects of the collider: glad you found the GetComponentInChildren function!
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Is there a convenient way to check if two gameobjects are touching BUT NOT overlapping? 2 Answers
Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 1 Answer
Multiple Cars not working 1 Answer
C# NullRefException accessing static class, array value 1 Answer