OnTrigger Problem

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

Guys, I’m having trouble with my game. Whenever object collides in my scene, it does not enter the if statement in my OnTrigger event. Even if my Match string variable it always says collided with the wrong object in the console. Please help. I need this for my Senior Project. Thanks in advance 🙂

This is my code for collision script. I am using 2D.

using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class Collide : MonoBehaviour
{
 public Text objText;
 private Text scoreText;
 public string Match;
public int counter;
 private int currentLine = 0;
public 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();
 theTextBox.startLine = currentLine;
 }
 void OnTriggerEnter2D (Collider2D col)
 {
 Match = objText.text.ToString();
 Debug.Log("Collision detected from "+ col.tag + " and " + Match);
if (col.tag.Equals(Match))
 {
 Debug.Log("Match, yay!");
 }
 else
 {
 Debug.Log("Collided with the wrong object" + col.tag + " " + Match);
 }
 }
void Update()
 {
 scoreText.text = counter.ToString();
 if (Input.GetMouseButton (0)) 
 {
 Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 pos.z = -1;
 trail.position = pos; 
 }
if(Input.GetKey(KeyCode.Space))
 {
 Debug.Log(Match);
 }
 }
} //class
  • Liked by
Reply