Hello everybody!
Old reader,new poster!
So i have this problem and i couldn’t find a solution for it,i wana set my gameobject active on button click,but once i relaunch the game,the button is setactive(false) back…I wana use playerprefs cause i know nothing about serialization and it’s my first game,this is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuyLightManager : MonoBehaviour {
public GameObject BoughtON;
void Start () {
}
public void BoughtLight ()
{
BoughON.SetActive (true);
}
void Update () {
}
}
Thanks in advance! Any answer is apreciated
Use code tags when posting code, it makes everything easier to read and discuss.
So you’ve got a typo (BoughON → BoughtON) that would prevent it from compiling.
To save it, you’ll use this at the same time you turn it on:
PlayerPrefs.SetInt("BoughtON", 1);
In Start(), you’ll want to use this:
BoughtON.SetActive(PlayerPrefs.GetInt("BoughtON", 0) == 1);
This essentially means, “check for BoughtON - default value of 0 if it doesn’t exist - and if that’s 1, set the thing to active”.
StarManta:
So you’ve got a typo (BoughON → BoughtON) that would prevent it from compiling.
To save it, you’ll use this at the same time you turn it on:
PlayerPrefs.SetInt("BoughtON", 1);
In Start(), you’ll want to use this:
BoughtON.SetActive(PlayerPrefs.GetInt("BoughtON", 0) == 1);
This essentially means, “check for BoughtON - default value of 0 if it doesn’t exist - and if that’s 1, set the thing to active”.
Thank you so much it helped me! I was searching for an answer for like 4 days!
actually this not work anymore
PlayerPrefs still works as well as it ever has. If it’s not working for you there’s something wrong with your code. Create a new thread, including your code in code tags.
I already did but i have no answer
This is my script. I tried anything with playerprefs but give me no result at all… I tried your method above and others…
public class CutScenesEnter : MonoBehaviour
{
public GameObject thePlayer;
public GameObject cutsceneCam;
public GameObject canvas;
public GameObject FPScanvas;
void Start()
{
PlayerPrefs.SetInt("toggle", 1);
}
void OnTriggerEnter(Collider _col)
{
if (_col.gameObject.CompareTag("Player"))
{
cutsceneCam.SetActive(PlayerPrefs.GetInt("toggle", 1) == 1);
thePlayer.SetActive(PlayerPrefs.GetInt("toggle", 0) == 1);
FPScanvas.SetActive(PlayerPrefs.GetInt("toggle", 0) == 1);
canvas.SetActive(PlayerPrefs.GetInt("toggle", 0) == 1);
this.gameObject.GetComponent<BoxCollider>().enabled = false;
cutsceneCam.SetActive(true);
thePlayer.SetActive(false);
canvas.SetActive(false);
FPScanvas.SetActive(false);
StartCoroutine(FinishCut());
}
}
IEnumerator FinishCut()
{
yield return new WaitForSeconds(4);
thePlayer.SetActive(true);
canvas.SetActive(true);
FPScanvas.SetActive(true);
cutsceneCam.SetActive(false);
PlayerPrefs.Save();
}
}
mahito2932:
I already did but i have no answer
This is my script. I tried anything with playerprefs but give me no result at all… I tried your method above and others…
public GameObject thePlayer;
public GameObject cutsceneCam;
public GameObject canvas;
public GameObject FPScanvas;
void OnTriggerEnter(Collider _col)
{
if (_col.gameObject.CompareTag("Player"))
{
this.gameObject.GetComponent<BoxCollider>().enabled = false;
cutsceneCam.SetActive(true);
thePlayer.SetActive(false);
canvas.SetActive(false);
FPScanvas.SetActive(false);
StartCoroutine(FinishCut());
}
}
IEnumerator FinishCut()
{
yield return new WaitForSeconds(4);
thePlayer.SetActive(true);
canvas.SetActive(true);
FPScanvas.SetActive(true);
cutsceneCam.SetActive(false);
PlayerPrefs.Save();
}
Your code doesn’t even use PlayerPrefs, read up on the documentation.
Updated code was the wrong one.
Your code overwrites your Active states in lines 27-30, it has nothing to do with PlayerPrefs not working.
when my player enters the trigger line nothing happens … if i remove 27-30 line
That’s probably because on Start you set the value, but you don’t actually Save the value until after your coroutine finishes.
Your code also only sets everything to Active when the “toggle” value is 1, and then sets most of them to Active again once the Coroutine finishes.
RadRedPanda:
That’s probably because on Start you set the value, but you don’t actually Save the value until after your coroutine finishes.
Your code also only sets everything to Active when the “toggle” value is 1, and then sets most of them to Active again once the Coroutine finishes.
I did something like this to define but still nothing what i’m actually doing wrong?
public class CutScenesEnter : MonoBehaviour
{
public GameObject thePlayer;
public GameObject cutsceneCam;
public GameObject canvas;
public GameObject FPScanvas;
void Start()
{
PlayerPrefs.SetInt("toggle", 0);
PlayerPrefs.SetInt("toggle2", 1);
PlayerPrefs.SetInt("toggle3", 1);
PlayerPrefs.SetInt("toggle4", 1);
}
void OnTriggerEnter(Collider _col)
{
if (_col.gameObject.CompareTag("Player"))
{
cutsceneCam.SetActive(PlayerPrefs.GetInt("toggle", 1) == 1);
thePlayer.SetActive(PlayerPrefs.GetInt("toggle2", 0) == 1);
canvas.SetActive(PlayerPrefs.GetInt("toggle3", 0) == 1);
FPScanvas.SetActive(PlayerPrefs.GetInt("toggle4", 0) == 1);
this.gameObject.GetComponent<BoxCollider>().enabled = false;
//cutsceneCam.SetActive(true);
//thePlayer.SetActive(false);
//canvas.SetActive(false);
//FPScanvas.SetActive(false);
StartCoroutine(FinishCut());
}
}
IEnumerator FinishCut()
{
yield return new WaitForSeconds(4);
thePlayer.SetActive(true);
canvas.SetActive(true);
FPScanvas.SetActive(true);
cutsceneCam.SetActive(false);
PlayerPrefs.Save();
}
That’s a great question! What are you doing wrong?
Well first of all you are posting to a FOUR YEAR OLD THREAD… that’s against forum rules. Please start your own fresh thread.
Second, when you post, please read this first:
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
BEFORE you post, get some information, do some research. To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
Kurt-Dekker:
That’s a great question! What are you doing wrong?
Well first of all you are posting to a FOUR YEAR OLD THREAD… that’s against forum rules. Please start your own fresh thread.
Second, when you post, please read this first:
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
BEFORE you post, get some information, do some research. To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
i already did but i never get answered
Btw i will hope that one day i can figure out
Hey iv been looking for this solution as you and when I came across this discussion it actually helped me and now it works completely fine your player prefs might be glitched from your other scripts do just use delete player prefs data it just deletes the saved data on playerprefs or if thus doesn’t work try making another script with only the player prefs code. Hope this helps