- Home /
How to make a UI Canvas not appear again after the first time the user hits the "I Agree" button.
In my app, I have a disclaimer. After the user hits the "I Agree" button, the disclaimer disappears. What I want is, that the user only has to press that button once, in the app's lifetime in his/her device. After the user agrees, the user will not see that disclaimer again. Meaning (forgive me if I am redundant) that, the user does not have to press the button (agree) every time he/she opens the app. Also after the game object is destroyed, I want to see the changes in the editor; in another script (because when the game object is destroyed you can't see it's inspector). This is the last part of the development of this app. The app is written in C#. Any help will be greatly appreciated.
Answer by applemaniac · May 19, 2015 at 10:48 AM
What you need is a global memory, to memorize if the user accepted your thing. You could use PlayerPrefs :
if (PlayerPrefs.GetString("accepted")=="true")
///Disable your canvas
if(///The player accepts)
PlayerPrefs.SetString("accepted", "true");
And you can still use your PlayerPref at any moment to know the changes.
$$anonymous$$uch appreciated applemaniac. The response was way too fast and very accurate. I feel like giving this simple, but useful code to the community. I searched for it in Unity Answers and found absolutely nothing specific, regarding how to make a one time disclaimer. So I will write a (SOLUTION) thread, that will read exactly like this: How to make a one time disclaimer. (SOLUTION) And of course, I will give you credit for it. Again, thanks for the time and attention you put in answering my question.
The name was changed to How to make a one time disclaimer. (C# SCRIPT) Here is the link
Since an ad$$anonymous$$ is fixed on rejecting posts without leaving a reason in a comment, like it is stated in the ad$$anonymous$$ guidelines for rejecting a post I am posting this here then.
Thanks to applemaniac, who demonstrated how to use PlayerPrefs, in a very accurate manner, and a very short notice.
TWO THINGS TO RE$$anonymous$$E$$anonymous$$BER:
The Application.Quit function is ignored in the editor. Test in the actual device.
Once you have tested the "one time property" of the script, you have to reset the script. So you can build the application with the disclaimer intact. In other words, there is no point in having a disclaimer, if the user will never see it, because you built the application with the disclaimer destroyed by yourself in the editor.
The "print" is there so you can see in the console, that the script worked. When the game object is destroyed, the inspector cannot display the contents of the object, because it is not there.
HOW TO RESET THE SCRIPT:
In UserAgrees() and Start(), set the int to 2. Save your script and return to Unity. Save your scene if you want and hit play. You will see your disclaimer is back. This is because the value used to destroy the disclaimer is a new one, and has not been met as a condition. Hit the "I Agree" button so the int (2) is saved. Return to your script, set the int to 1 again. Save your script and return to Unity. Now when you build, the disclaimer will be there in all the distributions of your app, because in each individual download, the condition used to destroy the disclaimer is yet to be met. I used the 2 only to demonstrate. You can just change the 1 to 2 and then build. Also I would like a "disclaimer" tag so it is even easier to find this answer. If a mod would be so kind to make one, I would appreciate it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DisclaimerController : $$anonymous$$onoBehaviour
{
public GameObject Disclaimer;
public Button iAgree;
public Button iDontAgree;
public void UserDoesNotAgree()
{
Application.Quit();
}
public void UserAgrees()
{
PlayerPrefs.SetInt ("accepted", 1);
Destroy (Disclaimer);
}
void Start ()
{
if (PlayerPrefs.GetInt ("accepted") == 1)
{
Destroy (Disclaimer);
print(PlayerPrefs.GetInt("accepted"));
}
}
}
Answer by anisharya16 · Jan 25, 2020 at 06:01 AM
I have the same problem, I created another canvas where i want to display game updates features. But that canvas does not shows up. I tries using "Sort Order" to 0 and 1 resp. but the new canvas does not shows up. How to solve this?