displaying images in canvas problem
Hi - first question here and beginner so please correct me if necessary.
I am working on my first very simple game where the player walks around the graveyard and reads the gravestones. That's it - pretty simple, right?
I have created a canvas that holds the images to be assigned to each gravestone. Upon clicking the space bar in front of each grave stone the image containing the text will be set to Activated thus appearing onscreen. I have a script where I assign the appropriate "image of text" to it's assigned gravestone.
I spent last night learning how to get the text to fade in and out as opposed to just appearing. I managed to get something that looks pretty nice! The problem I have now is that the text has some problems removing itself upon off-clicking when there is more than one gravestone with text running this script. It does work fine when only one gravestone is fully featured with this fading in of the "image of text". Here's the scripts I'm working with:
First is my "SignTwo" script. This is admittedly gleamed a bit from a youtube tutorial on sign text with some modifications to try to implement the fade feature.
public class SignTwo : MonoBehaviour { public GameObject talkBox; public bool playerInRange;
void Update() { if (Input.GetKeyDown(KeyCode.Space) && playerInRange) { if (talkBox.activeInHierarchy) { /* talkBox.SetActive(false); */ FindObjectOfType<Fade>().FadeMeOut(); } else { talkBox.SetActive(true); FindObjectOfType<Fade>().FadeMeIn(); } } } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { playerInRange = true; } } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Player")) { playerInRange = false; FindObjectOfType<Fade>().FadeMeOut(); } } ///method to be used in "Fade" script public void Off() { talkBox.SetActive(false); } }
Second is the "Fade" script which handles the fade feature:
public class Fade : MonoBehaviour { public void FadeMeOut() { StartCoroutine(fadeOut()); }
IEnumerator fadeOut() { yield return StartCoroutine(DoFade()); /*yield return new WaitForSeconds(2);*/ yield return StartCoroutine(Offing()); } IEnumerator DoFade() { CanvasGroup canvasGroup = GetComponent<CanvasGroup>(); while (canvasGroup.alpha > 0) { canvasGroup.alpha -= Time.deltaTime / 2; yield return null; } canvasGroup.interactable = false; } IEnumerator Offing() { yield return null; FindObjectOfType<SignTwo>().Off(); } public void FadeMeIn() { StartCoroutine(DoFadeIn()); } IEnumerator DoFadeIn() { CanvasGroup canvasGroup = GetComponent<CanvasGroup>(); while (canvasGroup.alpha < 1) { canvasGroup.alpha += Time.deltaTime / 2; yield return null; } canvasGroup.interactable = false; } }
I'm a little stuck here. I wonder if the problem has to do with the single script being applied to multiple objects which turns on and off multiple different images in a single canvas? I've tried duplicated the canvas, and putting each image in its own canvas but that creates more problems of images in some canvases not appearing at all, as well as not being set to "Active(false)" as the script would dictate.
I hope I've been somewhat clear. Can anybody help me understand where my problem might lie?
Thanks!