- Home /
How would I merge multiple GameObjects?
Hi everyone,
I'm currently creating a mix of pool and 2048 in Unity 2D, and I'm having some trouble combining GameObjects with the same values. Here is my code for this so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ValueBallManager : MonoBehaviour
{
public int bv;
public RectTransform rt;
public TextMeshPro tmp;
public Collider2D col;
void Start()
{
}
void Update()
{
tmp.text = "" + bv;
}
private void OnTriggerEnter2D(Collider2D collision)
{
string collisionValue;
TextMeshPro collisiontmp = collision.gameObject.GetComponent<TextMeshPro>();
collisionValue = collisiontmp.text;
if (collisionValue == tmp.text)
{
bv = bv * 2;
Debug.Log("hi!");
}
}
}
I'm using TextMeshPro to display the values since regular text wasn't working as well. Whenever I try and test this code, the console doesn't return anything. Is there something I'm doing wrong?
(I'm also really new to Unity so please excuse any lack of general knowledge I might demonstrate)
Try using
if (collisionValue.CompareTo(tmp.text) == 0)
Edit: In that case try
Debug.Log("TriggerEnter2D: " + collisionValue + " " + tmp.text);
if (collisionValue.CompareTo(tmp.text) == 0)
Nothing's happening still :/ thanks for the suggestion though.
I expected that to work but nope, the console's just blank. Maybe I've assigned something wrong?
line 20: bv.ToString() instead of cantenation line 32: you forgot to update text on tmp.text
The first part makes a lot more sense so I've put it in, but updating the text still doesn't work.
Answer by zORg_alex · May 25 at 07:16 AM
I'm not sure if they won't get destroyed both though.
private void OnCollisionEnter2D(Collider2D collision)
{
TextMeshPro myOtherBall = collision.gameObject.GetComponent<TextMeshPro>();
if (!myOtherBall || myOtherBall.bv != bv) return;
bv = bv * 2;
Destroy(myOtherBall.gameObject);
}