quick introduction to my project. I am trying to make a game in unity game engine and I am working on its shop system. Everything about the shop system is complete but one problem is still there. I need to save the State of button that is (MyButton) in my game scene and change it with (BuyButton) or (EquipButton) the saving part works but setting the button doesn't, I am using enum and switch statements for that part. These are the scripts of those two things.
script with the enum:
using UnityEngine;
using System.IO;
public class SaveEverything : MonoBehaviour
{
// What script to save
public LoadEveryThing MyButton;
public void Save()
{
// What data to save
string saveData = MyButton.state.ToString();
// Where the data is stored
string path = Application.persistentDataPath + "\buttonstate.save";
// Writes data to file
if (File.Exists(path))
{
File.WriteAllText(path, saveData/*, saveData1, saveData2, saveData3, 5*/);
}
else
{
File.Create(path).Close();
File.WriteAllText(path, saveData/*, saveData1, saveData2, saveData3, 5*/);
}
}
}
public enum ButtonState
{
Buy,
Equip
}
At the last part of script the enum is declared, with Buy and Equip inside it and rest of the script is about saving MyButton in a string.
Script with the switch statement:
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
using TMPro;
public class LoadEveryThing : MonoBehaviour
{
// Types of buttons
public GameObject BuyButton;
/* public GameObject BuyButton1;
public GameObject BuyButton2;
public GameObject BuyButton3;*/
public GameObject EquipButton;
/*public GameObject EquipButton1;
public GameObject EquipButton2;
public GameObject EquipButton3;*/
public ButtonState state;
void Start()
{
// What file to read save data from
string path = Application.persistentDataPath + "\buttonstate.save";
// Get data and set state to that data
if (File.Exists(path))
{
string data = File.ReadAllText(path);
if (Enum.TryParse(data, out ButtonState State))
state = State;
}
// Copy button properties onto this button
// depending on the state
switch (state)
{
case ButtonState.Buy:
SetButton(BuyButton);
break;
case ButtonState.Equip:
SetButton(EquipButton);
break;
}
if(shop.isSold == true)
{
state = ButtonState.Equip;
Debug.Log("working");
}
else if(shop.isSold == false)
{
state = ButtonState.Buy;
Debug.Log(" buy working");
}
}
void Update()
{
}
void SetButton(GameObject button)
{
// Set on-click method of button
Button myButton = GetComponent<Button>();
Button targetButton = button.GetComponent<Button>();
myButton.onClick = targetButton.onClick;
// Set text of button
TMPro.TextMeshProUGUI myText = GetComponentInChildren<TMPro.TextMeshProUGUI>();
TMPro.TextMeshProUGUI targetText = button.GetComponentInChildren<TMPro.TextMeshProUGUI>();
myText.text = targetText.text;
}
/*public static void SetEquipButton()
{
}
public static void SetBuyButton()
{
}*/
}
In the script the switch statement works like this if the enum ButtonState is ButtonState.Buy then it actives buy button else it active and sets Equip button. But the problem is I don't know how to change the ButtonScript State through script. So the switch statement doesn't have anything to change and save BuyButton to EquipButton. I had already tried this /*I made this variable of type ButtonState*/ public ButtonState state
and then I did this to change the enum State state = ButtonState.Equip;
but it is not changing the State of the enum. I can see that in the inspector and there are not errors as well. So what should I do?
This is the script where I am calling to change the enum state:
the script is too long so this is the method where I am calling it and I also as told I already made a variable of type public ButtonState state;
public class shop : MonoBehaviour
{
public TMPro.TextMeshProUGUI scoreText;
public GameObject Item1;
public GameObject Item2;
public GameObject Item3;
public GameObject Item4;
public TMPro.TextMeshProUGUI notenough;
public TMPro.TextMeshProUGUI notenough1;
public TMPro.TextMeshProUGUI notenough2;
public TMPro.TextMeshProUGUI notenough3;
public GameObject buyButton;
public GameObject buyButton1;
public GameObject buyButton2;
public GameObject buyButton3;
public GameObject equipButton;
public GameObject equipButton1;
public GameObject equipButton2;
public GameObject equipButton3;
public float sec = 14f;
public static bool isSold = false;
public static bool isSold1 = false;
public static bool isSold2 = false;
public static bool isSold3 = false;
public ButtonState state;
public ButtonState1 state1;
public ButtonState2 state2;
public ButtonState3 state3;
private Dictionary<GameObject, float> ItemPrices;
void Start ()
{
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
ItemPrices = new Dictionary<GameObject, float>()
{
{ Item1, 50f },
{ Item2, 80f },
{Item3, 3500f},
{ Item4, 5000f },
};
notenough.enabled = false;
notenough1.enabled = false;
notenough2.enabled = false;
notenough3.enabled = false;
}
public void PurchaseItem(GameObject Item)
{
foreach(KeyValuePair<GameObject, float> item in ItemPrices)
{
if (item.Key == Item)
{
float price = item.Value;
float val = PlayerPrefs.GetFloat ("Highscore");
if(val >= price)
{
val -= item.Value;
PlayerPrefs.SetFloat("Highscore", val);
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
buyButton.SetActive(false);
equipButton.SetActive(true);
isSold = true;
state = ButtonState.Equip;
// Take away the cost of the item from the player's currency
//PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
else
{
Debug.Log("not enough money");
notenough.enabled = true;
notenough1.enabled = true;
notenough2.enabled = true;
notenough3.enabled = true;
Invoke("noen", 2);
state = ButtonState.Buy;
}
}
}
}
public void noen()
{
notenough.enabled = false;
notenough1.enabled = false;
notenough2.enabled = false;
notenough3.enabled = false;
}
}
If any thing is unclear let me know.
Thanks,
question from:
https://stackoverflow.com/questions/65952869/calling-enum-state-to-change-correctly-but-it-is-not-changing-c-sharp-unity