Good morning, recently i started to make a game where, when the player enter in a trigger, a text appear and say him "Press F", when he press F a new message appears "Random Message" and the message that there where first disappears... but there is a problem... when i press F the old message doesnt disappear... why? Here's the script, (i applied this to the trigger) using System.Collections; using System.Collections.Generic; using UnityEngine; public class Livello1Discorsi : MonoBehaviour { public GameObject Subject; public GameObject Preview; void OnTriggerStay() { Preview.SetActive(true); if(Input.GetKeyDown(KeyCode.F)) { Subject.SetActive(true); Preview.SetActive(false); } } void OnTriggerExit() { Subject.SetActive(false); } }
On trigger stay is like a continuous loop while the object is inside the zone. Input. GetKeyDown, only fires once, so as soon as it's finished, Preview.SetActive(true) gets activated over and over in the loop. You should probably use OnTriggerEnter to set preview active.
here is what I might do Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Livello1Discorsi : MonoBehaviour { public GameObject Subject; public GameObject Preview; void OnTriggerEnter() { Preview.SetActive(true); } void OnTriggerExit() { Subject.SetActive(false); Preview.SetActive(false); } void Update() { if(Input.GetKeyDown(KeyCode.F) && Preview.activeSelf == true) { Subject.SetActive(true); Preview.SetActive(false); } } }