- Home /
Using OnTriggerEnter to change UI text
Hey so I'm building a baseball game and I'm using OnTriggerEnter to detect whether or not the baseball enters the strike zone. The strike zone is big enough so it can be detected and the ball isn't moving too fast. I want to display ("STRIKE!) once it hits the trigger, but nothing is happening. pls help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Strike : MonoBehaviour {
public AudioSource src;
public ParticleSystem stars;
public Text StrikeText;
private void Start()
{
StrikeText.text = " ";
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "ball")
{
src.Play();
Instantiate(stars, transform.position, transform.rotation);
StrikeText.text = "STRIKE!";
Destroy(other.gameObject);
SecondsWait();
StrikeText.text = " ";
}
}
IEnumerator SecondsWait()
{
yield return new WaitForSeconds(4);
}
}
Answer by BGOKMEN14 · Aug 06, 2018 at 05:31 AM
Hello @purne104!
I briefly looked at your code and I do not see any problem in it. However, I want you to check the collider attached to the strike zone. Make sure "is trigger" is checked and let me know if it works or not.
I hope this helped :)
Your answer
Follow this Question
Related Questions
Help with making a triggered gui message 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
String.Remove not working 1 Answer
Respawn from falling off world! 6 Answers