How can I change one canvas image to another?
So probably answer is near to my question but I can't figure it out. I have in my game small instructions. If player first time coming to some BoxCollider2D he can see first of two Canvas images. After he can press Space and this image should hide forever for this object and instead show another one.
//This class keeping methods
public class DialogManager : MonoBehaviour {
public GameObject dialogBox1;
public GameObject dialogBox2;
public Text dialogText1;
public Text dialogText2;
public bool dialogActive;
// Update is called once per frame
void Update () {
if (dialogActive && Input.GetKeyDown(KeyCode.Space)) {
dialogBox1.SetActive(false);
dialogActive = false;
}
}
//First Time
public void ShowBox1(string dialog) {
dialogActive = true;
dialogBox1.SetActive(true);
dialogText1.text = dialog;
}
//Another case
public void ShowBox2(string dialog)
{
dialogBox2.SetActive(true);
dialogText2.text = dialog;
}
}
//And this one FirstTImeDialogHolder public class FTDialogHolder : MonoBehaviour {
public string dialog1;
public string dialogOther;
public GameObject instructImg;
private bool firstTime = true;
private DialogManager dialogManager;
private bool isTouching;
// Use this for initialization
void Start()
{
dialogManager = FindObjectOfType<DialogManager>();
}
// Update is called once per frame
void Update()
{
if (isTouching && !firstTime)
{
dialogManager.ShowBox2(dialogOther);
}
else
{
instructImg.SetActive(false);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (firstTime)
{
if (other.gameObject.name == "Player")
{
dialogManager.ShowBox1(dialog1);
}
}
if (other.gameObject.name == "Player")
{
isTouching = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.name == "Player")
{
firstTime = false;
isTouching = false;
}
}
}
Like i said at the start, answer is near to question. This scripts work perfect, and do what i exactly want to see...but for one of third different objects with this script. So all three objects have FTDialogHolder script and showing first one image (at the bottom of the screen). But the second one image (top-right corner) showing only for one of three objects (And yeah, i can have even more than 3 objects).
Canvas picture. At the start images are active(false).
Answer by UnityCoach · Nov 05, 2018 at 05:17 PM
You want to save that state to PlayerPrefs. Something in the like :
void Awake ()
{
bool firstTime = PlayerPrefs.GetInt ("FirstTime", 1) == 1;
firstTimeDialogObject.SetActive(firstTime);
otherDialogObject.SetActive(!firstTime);
}
And of course, you need to save it at some point, using
PlayerPrefs.SetInt ("FirstTime", 0);
While I'm writing to you that its didn't help, I'm figure out that my script upper than box collider xDDDD That's why it didn't work. But from your answer i'm open for myself new option (PlayerPrefs static methods). Thanks a lot! xD
Yes, I often wrap PlayerPrefs in a static class as static accessors. Like
public static class GameSettings
{
private static float _musicVolume = -1;
public static float musicVolume
{
get
{
if (_musicVolume == -1) // if not initialised
_musicVolume = PlayerPrefs.GetFloat ("$$anonymous$$usicVolume", 0.5f); // get or set default
return _musicVolume;
}
set
{
if (_musicVolume != value) // if value changes
{
_musicVolume = value;
PlayerPrefs.SetFloat ("$$anonymous$$usicVolume", _musicVolume);
}
}
}
}
Your answer
Follow this Question
Related Questions
Activating multiple images under one canvas via multiple objects 0 Answers
Changing order in layers at runtime with a coroutine 0 Answers
create calendar in canvas using c# 0 Answers
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers
Unity 2D Build Error 0 Answers