TriggerEnter2D – Update Text

Updated on June 9, 2017 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on June 9, 2017

Hey, guys. I have a problem about my game.

About the game:

You have to collide with the object that matches the text shown in the screen.
When the text changes, the object that the player needs to collide with will also change.

Problem:

At start, the text shown is “Cherry”. I’m using them as Trigger. When the player collides with the cherry the counter increments,
but a message will show if the player is mistaken. But when the text changes to “Lime” and I collided
with the fruit Lime, it still says that I am colliding with the wrong fruit/object. However the counter still increments if I collided with a Cherry.

How can I change or update the tag that the player needs to collide with for every object in the OnTrigger Event?
Any help would be appreciated. Thanks in advance and more power to this group 🙂

PS. I’m using Unity 5.5

This is my collision script.

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

public class Collide : MonoBehaviour
{
private Text objText;
private Text scoreText;
private string Match;
private int counter;

private int currentLine;
// private int endLine;

private TextManager theTextBox;
public Transform trail;

void Start()
{
objText = GameObject.Find(“DescText”).GetComponent<Text>();

scoreText = GameObject.Find(“ScoreText”).GetComponent<Text>();

theTextBox = FindObjectOfType<TextManager>();

Match = objText.text.ToString ();
}

void OnTriggerEnter2D (Collider2D col)
{
//this is a reference for the array of strings in my text manager script
theTextBox.startLine = currentLine;

if (col.tag == Match) {
counter++;
Debug.Log(counter);
col.gameObject.SetActive (false);
if (counter == 3) {
currentLine++;
//FruitSpawn.current.objectPools [0] = FruitSpawn.current.extraPools [0];
}
}
else
{
col.gameObject.SetActive (false);
Debug.Log (“Wrong fruit”);
}
}

void Update()
{
scoreText.text = counter.ToString();

if (Input.GetMouseButton (0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
pos.z = -1;
trail.position = pos;
}

}

} //class

  • Liked by
Reply