I am trying to change the state of a enum through bool using if statement. Then what it will do is make a switch statement work. There are three scripts 1. LoadEveryThing sript with the switch statement 2. shop script with the bool 3. SaveEverthing script where the enum exists, I made a variable of type of the enum and then changing its state according to the state of the bool. Then on the basis of the enum state the switch statement works. Here are the scripts.
shop script:
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;
public static bool isSold1;
public static bool isSold2;
public static bool isSold3;
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;
}
}
isSold is the bool here.
SaveEverything script:
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
}
ButtonState is the enum.
LoadEveryThing script:
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()
{
}*/
}
So now I wrote a variable of type ButtonState named state. Then in a if statement I checked whether the isSold bool is true or false if it is true change the state = ButtonState.Equip else if it is false change the ButtonState to buy. After all this stuff the switch statement works according to it. If ButtonState is buy then change the button to buybutton else changes it to equipbutton. But the problem is the enum state is not changing and I think the problem can be in the if statement or I don't know. By the way I am doing this stuff in c# using Unity Game Engine, That's all. If anything is unclear let me know, pls help. Thanks,
Aucun commentaire:
Enregistrer un commentaire