Location Does Not Change
I've been trying to create a script that tells you the location that you are in at the top of the screen with text on top of a banner, and I came up with this code:
using UnityEngine;
using UnityEngine.UI;
namespace RPG.Core
{
public class ShowUIText : MonoBehaviour
{
[SerializeField] Text locationText = null;
[SerializeField] Image topBanner = null;
[SerializeField] Image topBannerOutline = null;
[SerializeField] string location = null;
[SerializeField] float bannerTransitionTime = 1.0f;
void Start()
{
topBanner.CrossFadeAlpha(0f, 0f, false);
topBannerOutline.CrossFadeAlpha(0f, 0f, false);
locationText.CrossFadeAlpha(0f, 0f, false);
locationText.text = location;
}
void OnTriggerEnter()
{
BannerAppear();
}
void OnTriggerExit()
{
BannerHide();
}
public void BannerAppear()
{
topBanner.CrossFadeAlpha(1f, bannerTransitionTime, false);
topBannerOutline.CrossFadeAlpha(0.5f, bannerTransitionTime, false);
locationText.CrossFadeAlpha(1f, bannerTransitionTime, false);
}
public void BannerHide()
{
topBanner.CrossFadeAlpha(0f, bannerTransitionTime, false);
topBannerOutline.CrossFadeAlpha(0f, bannerTransitionTime, false);
locationText.CrossFadeAlpha(0f, bannerTransitionTime, false);
}
}
}
I attached this script to several empty game objects with Is Trigger checked off and specified a different location for each. They were: (In this same order) - Town Center - Courtyard - Wizard's Tower - Graveyard
Now whenever I play it displays the banners correctly, but it only displays one location (Graveyard) when I go into completely different locations (EX: Town Center, Courtyard)
Answer by Hatsuko · Oct 26, 2017 at 07:33 PM
I played a bit with your script and it worked :) In the scene, are all of your ShowUIText reference to the same Text and Image? If so then you need to bring this line locationText.text = location;
from Start() to BannerAppear(), since every time your banner appears, it needs to refresh what it's displaying, so that you get different location displayed.
Thanks! I cannot believe how simple this error was! XD