enabling and disabling text with key press.
Hello, I have a situation where a door should be unlocked with the key. But if the player tries to open the door with out a key, It should display "You Need a Key", and the text should gets disabled.
But the text is showing up only once. I want the text to appear the number of times the player tries to open the door. Pls help with c#. Thank you
using UnityEngine; using System.Collections;
public class Door : MonoBehaviour { public bool open = false;
public float doorOpenAngle = -90f;
public float doorCloseAngle = 0f;
public float smooth = 2f;
private AudioSource audioSource;
public AudioClip openingSound;
public AudioClip closedSound;
public bool isLocked = false;
public GameObject note;
// Use this for initialization
void Start ()
{
audioSource = GetComponent<AudioSource>();
note.SetActive(false);
}
public void ChangeDoorState()
{
if(isLocked != true)
{
open = !open;
if(audioSource != null)
{
audioSource.PlayOneShot(openingSound);
}
}
else
{
audioSource.PlayOneShot(closedSound);
note.SetActive(true);
Destroy(note, 1);
}
}
// Update is called once per frame
void Update ()
{
if(open)
{
Quaternion targetRotationOpen = Quaternion.Euler(0, doorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotationOpen, smooth * Time.deltaTime);
}
else
{
Quaternion targetRotationClosed = Quaternion.Euler(0, doorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotationClosed, smooth * Time.deltaTime);
}
}
}
Answer by Mmmpies · Jun 12, 2016 at 09:33 AM
You're destroying the note rather than waiting a second and using set active to false.
Once destroyed the note no longer exists so you can't set active.
A better approach would be to record Time.time when you set the message to active and set to bool to true. so have a float and and a bool.
private float messageDisplayTime;
private bool messageDisplaying;
then when you set the message to active.
note.SetActive(true);
messageDisplayTime = Time.time;
messageDisplaying = true;
And have an if to check that bool and compare the time.
if(messageDisplaying)
{
if((Time.time - messageDisplayTime) > 1f)
{
note.SetActive(false);
messageDisplaying = false;
messageDisplayTime = 0f;
}
}
Change the 1f to however many seconds you want the message to display.
Your answer
Follow this Question
Related Questions
How do You add a Scrollbar in a textbox C# code 0 Answers
Help with arrays of arrays please 2 Answers
Input Field Problem 0 Answers
uGUI Text draw calls 1 Answer
Need help with UI.text 1 Answer