show instrution image only once

hi,
i have a ui image which is instruction at beginning of game.
this image has a simple 2d drag animation and is disabled after some seconds.

using UnityEngine;
using System.Collections;

public class ShowHelpDisabled : MonoBehaviour
{

    public GameObject objectToDisable;
    public GameObject objectToDisable02;

    private void Start()
    {
        StartCoroutine(ActivationRoutine());
    }

    private IEnumerator ActivationRoutine()
    {       
        yield return new WaitForSeconds(6);

        objectToDisable.SetActive(false);
        objectToDisable02.SetActive (false);
    }
}

i need it to be disabled the next time player start to load scene and play the game.please help me.thanks.

Use PlayerPrefs to set a flag on the first start. Query flag on subsequent starts.

i did , but where do you mean by subsequent starts?

using UnityEngine;
using System.Collections;

public class ShowHelpDisabled : MonoBehaviour
{
    private int showhelp;

    public GameObject objectToDisable;
    public GameObject objectToDisable02;

    private void Start()
    {
        showhelp = ShowHelpDisabled.showhelp;
        int showhelp = PlayerPrefs.GetInt ("showhelp");
        int showhelp = PlayerPrefs.SetInt ("showhelp");


        StartCoroutine(ActivationRoutine());
    }

    private IEnumerator ActivationRoutine()
    {       
        yield return new WaitForSeconds(6);

        objectToDisable.SetActive(false);
        objectToDisable02.SetActive (false);
    }
}
if(PlayerPrefs.HasKey("key"))
{
    // Not the first start because pref exists
    // Don't show help
}
else
{
    // First start because pref does not exist
    // Create pref
    PlayerPrefs.SetInt("key",0);
    PlayerPrefs.Save();
    // Show help
}

does not work . i am beginner . please explain where to put code. thanks.

using UnityEngine;
using System.Collections;

public class ShowHelpDisabled : MonoBehaviour
{
    private int showhelp;

    public GameObject objectToDisable;
    public GameObject objectToDisable02;

    private void Start()
    {

        StartCoroutine(ActivationRoutine());

        if(PlayerPrefs.HasKey("showhelp"))
        {
            // Not the first start because pref exists
            // Don't show help
        }
        else
        {
            // First start because pref does not exist
            // Create pref
            PlayerPrefs.SetInt("showhelp",0);
            // Show help
        }
    }

    private IEnumerator ActivationRoutine()
    {       
        yield return new WaitForSeconds(6);

        objectToDisable.SetActive(false);
        objectToDisable02.SetActive (false);
        PlayerPrefs.SetInt ("showhelp",0);

    }
}

My snippet was an example and is not going to do anything on its own. You need to use just a little bit of initiative here. The comments should tell you all you need to know.

1 Like

on which method your snippet should be placed?

You’re almost there but you’re still calling your activation routine each time - which you don’t want to do. My comments tell you when to call it.

1 Like

i have there coroutine , what do you mean by show help? it means : PlayerPrefs.SetInt(“key”,0);
???

and dont show help??? strange!

Ok, sorry I misread your code. My code snippet is showing you how to use PlayerPrefs to set/query a flag in order to decide what to do. You could disable your objects in Awake and then only activate them the first time, i.e. when the pref does not exist, before disabling again 6 seconds later. After the first time, when the pref does exist, you do nothing because the objects have already been disabled in Awake.

exactly i wanted to say that. here with DisableRoutine , after 6 seconds two gameobjects are disabled.

now with help of your snippet and pointing to awake method , will you by code language clarify it alittle so that i follow the way? thanks.

P.S : i changed ActivationRoutine to DisableRoutine so that it is better understood.

void Awake()
{
    // Disable objects here
}

void Start()
{
        if(PlayerPrefs.HasKey("showhelp"))
        {
            // Not the first time because pref already exists
            // Do nothing here
        }
        else
        {
            // First time because pref does not exist
            // Create pref
            PlayerPrefs.SetInt("showhelp",0);
            // Call coroutine here
        }
}

IEnumerator MyCoroutine()
{
    // Enable objects here

    // Wait for 6 seconds here

    // Disable objects here
}

with no error , it does not work!

using UnityEngine;
using System.Collections;

public class ShowHelpDisabled : MonoBehaviour
{
    private int showhelp;

    public GameObject objectToDisable01;
    public GameObject objectToDisable02;

    void Awake()
    {
        objectToDisable01.SetActive(false);
        objectToDisable02.SetActive (false);
    }

     void Start()
    {
        if(PlayerPrefs.HasKey("showhelp"))
        {
            // Not the first time because pref already exists
            // Do nothing here
        }
        else
        {
            // First time because pref does not exist
            // Create pref
            PlayerPrefs.SetInt("showhelp",0);
            StartCoroutine(DisableRoutine());       
        }
    }

     IEnumerator DisableRoutine()
    {     
        objectToDisable01.SetActive(true);
        objectToDisable02.SetActive (true);
        yield return new WaitForSeconds(2);
        objectToDisable01.SetActive(false);
        objectToDisable02.SetActive (false);

    }
}

what is wrong?

You don’t say what is wrong but I would assume that the problem is because the pref already exists from earlier. Try a different key name or delete the pref.

yes . in another script i use playerpref but with different key like score . also in your code there is no getint …
one question : private int showhelp; is an int, right? but why key is string?

look , this is all :
there is a gameobject with a script attachd to. inside script there is startcoroutine and disables the gameobject after 2 seconds.
now the queston is how to see this gameobject only one time and next time scene is loaded, that gameobject does not show up?

In my example I’m just using the existence of the pref as a flag. It doesn’t have to be an int and can be any supported type because I don’t care what the value is. All I care about is whether it exists or not. Key is always a string - see the docs.

i dont know what to do.

It’s not clear what you’re actually trying to achieve but if you want the same thing to happen each time your game runs then you don’t even need prefs, you just need a flag that gets set on the first time. Probably easiest is a static bool in your script.

static bool flag = false;

void Awake()
{
    // Disable objects here
}
void Start()
{
        if(!flag)
        {
            flag = true;
            // Call coroutine here
        }
}
IEnumerator MyCoroutine()
{
    // Enable objects here
    // Wait for 6 seconds here
    // Disable objects here
}

what i am trying to achieve is to disable forever a gameobject in my game after it is showed only one time , no more i need it to be showed. only one time as a help image. this gameobject has a coroutine to stay in scene for 2 seconds.