Check if flashlight is picked up

So, I’m trying to do an script that would check from PlayerPrefs at the start of every map if Player has picked up an flashlight from first map, and if he has picked it up it’s on automaticly so you don’t need to pick it up on every map.

Here’s the flashlight pickup script:

#pragma strict

var flashlight : GameObject;

function OnTriggerEnter(other : Collider) 
{
    //
    if(other.gameObject.tag == "Player")
        {
            Debug.Log("Picked up flashlight");
            PlayerPrefs.SetInt("Flashlight", 1);
            Debug.Log("Flashlight = 1");
            flashlight.SetActive(true); 
            Destroy (gameObject);

        }
}

And here’s the script that I’m using to check from PlayerPrefs if Player has picked it up from first level or not.

#pragma strict

var FlashLight1 : int = 0;
FlashLight1 = PlayerPrefs.GetInt("Flashlight");
var flashlight : GameObject;

function Start () {
if(FlashLight1 == 1)
        {  					
            flashlight.SetActive(true);
        }
else if(FlashLight1 == 0)
        {
        	flashlight.SetActive(false);
        }

}

function Update ()
{
if(FlashLight1 == 1)
        {  					
            flashlight.SetActive(true);
        }
else if(FlashLight1 == 0)
        {
        	flashlight.SetActive(false);
        }
}

But this doesn’t work very well. Because the PlayerPrefs.SetInt(“Flashlight”) is 0 at start I can’t see that object at all on first level, so I can’t even pick it up which would set it on 1, and then it would be enabled on every level as far as I know.

Is there some more clever way to keep that data (that player has flashlight) even when I load next level so player won’t have to pick it up from every level again and again.

Sorry If I was being unclear with my problem but I don’t really know how to explain it any better. And sorry that I don’t have any comments on code, but those scripts should be easy enough to understand.

Thank you in advance!

You could try DontDestroyOnLoad (flashlight) when they collect it, assuming that the next level is a new scene. Basically, the game object that has DontDestroyOnLoad () is not destroyed automatically when loading a new scene.

This code looks like it should work.

You can check if the player has ever picked up flashlight to see if you should spawn one by using a default value.

var FlashLight1 = PlayerPrefs.GetInt("Flashlight",0);

Then if its 0 you need to spawn a flashlight/dont destroy one on that map.

And set it to 0 when the player starts the first scene or presses start in the menu in case it has been set before.

Another option is using DontDestroyOnLoad(GameObject).On your player or manager object so it keeps all the vars between scenes.

I just did it using another way, that you don’t have to pick it up at all and you just activate it with F button, because this still allows me to do an achievement if player plays whole game without activating flashlight.

I wen’t with this way because no matter what I tried I couldn’t get it work like I wanted so I used this to overcome that problem + it might be even better like this from game design point of view.

Thanks to everyone though.