How can I save the playerprefs of a boolean variable? - c#

I am trying to code a mute button, and it works, however the setting of that button is not saved.
public void Mute (){
AudioListener.volume = 0;
sound.enabled = false;
sound.image.enabled = false;
noSound.enabled = true;
noSound.image.enabled = true;
}
public void UnMute (){
AudioListener.volume = 1;
sound.enabled = true;
sound.image.enabled = true;
noSound.enabled = false;
noSound.image.enabled = false;
}
I have 2 methods here, and I need it to save the state of which one was clicked using playerprefs. I was thinking something along the lines of a boolean, but I'm stumped, and and I can't wrap my head around how I would do that.

Yes you can do that with the help of a boolean variable as like the following:
public bool isMuted = false;
public void Do_muteOperation()
{
if (isMuted)
{
UnMute();
isMuted = false;
}
else
{
Mute();
isMuted = true;
}
}

Related

Object reference not set to an instance of an object (Cannot Loot Object) C# Unity

Hello i have 2 scripts one is for Flashlight "Battery UI script" Storing how many batteries i have left
for reload a flashlight max 5 that script is working totally fine but when i try to loot a battery it occur a error
and i dont understand why because i setup input "Use" in Project Settings correctly because another key "Grab" is working fine on Key "Q" but "Use" on Key "F" not working and i getting this error what i do wrong?!
i will paste code bellow with screenshots as well
" Get Error in this line (see screenshot) "
Click for code screenshot error
battery GUI on top right corner 3/5 collected batteries
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BatteryPickup : MonoBehaviour {
private Transform myTransform;
private GameObject MessageLabel;
private GameObject BatteryUIScript;
public bool EnableMessageMax = true;
public bool Enabled;
public float BatteryAdd = 0.01f;
public AudioClip pickupSound;//sound to playe when picking up this item
public string MaxBatteryText = "You have Max Batteries";
public Color MaxBatteryTextColor = Color.white;
public bool PickupMessage;
public string PickupTEXT = "Battery +1";
public Color PickupTextColor = Color.white;
void Start () {
myTransform = transform;//manually set transform for efficiency
}
public void UseObject (){
BatteryUIScript = GameObject.Find("Flashlight");
BatteryUI BatteryComponent = BatteryUIScript.GetComponent<BatteryUI>();
if (BatteryComponent.EnableBattery == true)
{
Enabled = true;
}
if(BatteryComponent.EnableBattery == false){
Enabled = false;
if(EnableMessageMax){StartCoroutine(MaxBatteries());}
}
if(Enabled){
StartCoroutine(SendMessage());
BatteryComponent.Batteries += BatteryAdd;
if(pickupSound){AudioSource.PlayClipAtPoint(pickupSound, myTransform.position, 0.75f);}
this.GetComponent<Renderer>().enabled = false;
this.GetComponent<Collider>().enabled = false;
}
}
public IEnumerator SendMessage (){
MessageLabel = GameObject.Find("UI_MessageLabel");
Text Message = MessageLabel.GetComponent<Text>();
/* Message Line */
EnableMessageMax = false;
Message.enabled = true;
Message.color = PickupTextColor;
Message.text = PickupTEXT;
yield return new WaitForSeconds(2);
Message.enabled = false;
EnableMessageMax = true;
}
public IEnumerator MaxBatteries (){
MessageLabel = GameObject.Find("UI_MessageLabel");
Text Message = MessageLabel.GetComponent<Text>();
/* Message Line */
if(!Enabled){
EnableMessageMax = false;
Message.enabled = true;
Message.color = MaxBatteryTextColor;
Message.text = MaxBatteryText;
yield return new WaitForSeconds(3);
Message.CrossFadeAlpha(0f, 2.0f, false);
yield return new WaitForSeconds(4);
Message.enabled = false;
EnableMessageMax = true;
}
}
}

How to use timer in c# windows form to move elevator up and down only when button is pressed?

I have been trying move elevator up and down in c# using timer when button is pressed but somehow I failed in that task. Below is the code I was trying use to make it happen.Would you please help me out?
private void timer_elevator_go_down_Tick(object sender, EventArgs e)
{
if (elevator.Top <= y_ff)
{
elevator.Top += 1;
}
else
{
timer_elevator_go_down.Enabled = false;
btn_down.Enabled = true;
btn_G.Enabled = true;
btn_up.BackColor = Color.Red;
btn_1.BackColor = Color.Red;
arrived_G = true;
elevator.Image = global::PLift_Control.Properties.Resources.elevator;
}
}
Here following variables I declared.
public partial class ElevatorControl : Form
{
int y_gf = 100;
int y_ff = 347;
bool go_up = false;
bool go_down = false;
bool arrived_G = false;
bool arrived_1 = false;
The problem there is that you're trying to change the location using the wrong method. Here's how you change the location:
elevator.Location = new Point(elevator.Location.X, elevator.Location.Y + 1);
// +1 being whatever amount you would like to increase/decrease it by

boolean not updating with code in Unity

I have a script that changes the score randomly. A and B. I want the scores to change the button type from false to true depending on the condition. For some reason it won't change.
Unless the scores are even, the last script should continually change the true or false. I also only want these to last until the next button is pushed.
I have printed out the information, so I know the scores are being pulled in they just don't seem to be calculating.
public static bool yayButton = false;
public static bool booButton = false;
ScoreManager.scoreDisplayA;
ScoreManager.scoreDisplayB;
if(ScoreManager.scoreDisplayA > ScoreManager.scoreDisplayB){
yayButton = true;
}
if(ScoreManager.scoreDisplayB > ScoreManager.scoreDisplayA){
yayButton = true;
}
You set yayButton to true in both condition, So this is your first mistake.
With this code once one of your variable become true, it always will be true so I think you need to turn other variable to false.
And this code must be in Update() or FixedUpdate() method to detect new change of ScoreManager.
Your final code should be something like this:
public static bool yayButton = false;
public static bool booButton = false;
ScoreManager.scoreDisplayA;
ScoreManager.scoreDisplayB;
void Start()
{
// define the ScoreManager.scoreDisplayA and B here.
// Something like this:
// ScoreManager.scoreDisplayA = GameObject.Find("ScoreManager").GetComponent<scoreDisplayA>();
}
void Update()
{
if(ScoreManager.scoreDisplayA > ScoreManager.scoreDisplayB){
yayButton = true;
booButton = false;
}
if(ScoreManager.scoreDisplayB > ScoreManager.scoreDisplayA){
yayButton = false;
booButton = true;
}
}

A pause menu using a button and click

I'd like to know how I can pause and unpause menu from the same button using the mouse pointer when I click on it.
Lets say I have this. C#
void Update () {
if (Button_Pause.OnPointerClick()) {
if(!active){
PauseGame();
}
else{
ResumeGame();
}
active = !active;
}
}
public void PauseGame()
{
Button_Pause = Button_Pause.GetComponent<Button> ();
Canvas_PauseMenu.enabled = true;
Button_Exit.enabled = true;
Button_Pause.enabled = true;
}
public void ResumeGame()
{
Canvas_PauseMenu.enabled = false;
Button_Exit.enabled = false;
Button_Pause.enabled = false;
}
In the first line, where I call the OnPointerClick I'm just guessing because I don't know what to do. What I've searched around, using click to show something it's having a TimeScale or something like that.
¿Can anyone help moi? Please.
Add a listener for your button and in your pause script set timescale to zero to pause the game
[SerializeField] private Button MyButton = null; // assign in the editor
void Start() { MyButton.onClick.AddListener(() => { pause();});
}
void pause(){
if (Time.timeScale == 1)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
I managed to solve the issue. It might not be efficient but it does what I need.
I created 2 buttons in the same place. Those buttons are represented with different sprites (Pause & Play). "Pause" is visible since the start. When I click on it, the menu pops up, the "Pause" stop being active and the "Play" sprite button activates and pops up too. When I click on it, I unpause and goes back to the "Pause" sprite visible in the screen.
void Start () {
Canvas_PauseMenu = Canvas_PauseMenu.GetComponent<Canvas> ();
Button_Pause = Button_Pause.GetComponent<Button> ();
Button_Resume = Button_Resume.GetComponent<Button> ();
Canvas_PauseMenu.enabled = false;
Button_Resume.enabled = false;
Button_Resume.gameObject.SetActive (false);
}
// Update is called once per frame
public void PauseTest () {
if(!active){
PauseGame();
}
else{
ResumeGame();
}
}
public void BackToMainMenu()
{
Application.LoadLevel (0);
}
public void PauseGame()
{
Canvas_PauseMenu.enabled = true;
Button_Exit.enabled = true;
Button_Pause.enabled = false;
Button_Pause.gameObject.SetActive (false);
Button_Resume.enabled = true;
Button_Resume.gameObject.SetActive (true);
active = true;
Time.timeScale = 0;
}
public void ResumeGame()
{
Canvas_PauseMenu.enabled = false;
Button_Exit.enabled = false;
Button_Pause.enabled = true;
Button_Pause.gameObject.SetActive (true);
Button_Resume.enabled = false;
Button_Resume.gameObject.SetActive (false);
active = false;
Time.timeScale = 1;
}

How can I check if the level is loaded in Unity3D

I've started learning C# and Unity3D this year and I've come across a problem. I'm loading a new level and the player object gets initialized before the the level has finished loading.
I think I only need to check whether the level is loaded or not before initializing the object. I don't know if I could use LoadLevelAsync. I'm using Unity3D Personal Edition.
My current code:
MasterClient.cs
void Update(){
if(SceneUpdateRequired)
{
Application.LoadLevel(SceneUpdateID);
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
}
}
void CharacterLoginUpdate(){
if (SceneUpdateCompleted == true)
{
GameObject networkPlayerObj = (GameObject)Instantiate(NPlayerObj, PlayerLoginUpdatePosition, PlayerLoginUpdateRotation);
NPlayer networkPlayer = networkPlayerObj.GetComponent<NPlayer>();
networkPlayer.UpdatePlayerInstantiateCompleted(PlayerLoginUpdateNetworkID, PlayerLoginUpdateHP, PlayerLoginUpdateClass, PlayerLoginUpdateLevel, PlayerLoginUpdateName, PlayerLoginUpdatePosition);
PlayerLoginUpdateRequired = false;
}
}
The problem with your code is your are loading level in update where if you dont control the task with booleans you are going to end up with big problems.
your update code should be as follows
void Update(){
if(SceneUpdateRequired)
{
Application.LoadLevel(SceneUpdateID);
SceneUpdateRequired = false;
}
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
}
This way the scene code will try to load level only when you request it to and not every time in update loop as loading levels is heavy.
Another thing is you might encounter one more problem if you need to use
SceneUpdateRequired,SceneUpdateCompleted variables somewhere else the
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
the above part which is in update loop will reset the variables everytime whenever its not loading the level. To prevent this you will have to introduce another boolean flag to stop its update happening everytime. So your code will end up looking like this if you want to prevent everyframe update
void Update() {
if(SceneUpdateRequired){
Application.LoadLevel(SceneUpdateID);
SceneUpdateRequired = false;
}
if (Application.isLoadingLevel == false && StartCheckingLoadLevel){
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
StartCheckingLoadLevel = false;
}
}
void StartLoad() {
SceneUpdateRequired = true;
SceneUpdateCompleted = false;
StartCheckingLoadLevel = true;
}
void CharacterLoginUpdate(){
if (SceneUpdateCompleted == true) {
Debug.Log("Do Something after load");
} else {
Debug.Log("Scene not yet loaded");
}
}
Hope this helps.

Categories

Resources