Triggers2D

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 :slight_smile:

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();

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

theTextBox = FindObjectOfType();

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

It’s pretty hard to read without code tags.

Sorry about that. I’m still new to the forum so I don’t know about it.

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

It wuld have been a bit nicer to edit your first post for code tags vs. quoting … but still better than nothing.
Okay, it looks like ‘Match’ is never updating.

Additionally, it would be an improvement to set the scoreText.text at the time you actually change the score, rather than running it in every update call :slight_smile:

Sorry about that. Anyway, how do I update the ‘Match’? I tried putting it in where the counter reaches 3 but it is still not working.

And for the score, I’ll do that. Thanks for the suggestion :slight_smile:

Well, I think that you have to update the match string when the object you’re supposed to find has changed, right?
So, wherever that happens :slight_smile:

OK, thanks for the reply :slight_smile:

Did you get this fixed? :slight_smile: