Store separate values from a script on each identical prefab
Hello, currently I am trying to create a set of scripts that will allow the player to walk up to a prefab, interact with it, and walk away. These identical prefabs will be spread throughout the level. The issue I am having is that when the player walks up to a prefab, the information is saved across all instances of the prefab and I need each prefab to store separate values. Currently, this script is on the player, when they walk up to a instance of the prefab it will open up a menu and options and values will be stored from it. I'm not sure how to make each prefab store separate values, or if it's possible. I'm sure there is a better way to do what I'm currently doing but I'm somewhat new to Unity and C#.
public class BattleUIManager : MonoBehaviour
{
public Transform trick1;
public Transform trick2;
public Button option1B;
public Button option2B;
public Slider energySlider;
public Slider happySlider;
public bool trick1Once;
public bool trick2Once;
bool doingTrick = false;
public bool activeVillager = false;
public int Energy;
public int Happiness;
void Start()
{
trick1Once = true;
trick2Once = true;
Button opt1 = option1B.GetComponent<Button>();
Button opt2 = option2B.GetComponent<Button>();
opt1.onClick.AddListener(Option1);
opt2.onClick.AddListener(Option2);
energySlider.value = 1;
}
void Option1()
{
int energyCost = 10;
if (Energy - energyCost >= 0 && trick1Once)
{
doingTrick = true;
Energy -= energyCost;
Instantiate(trick1);
StartCoroutine(ExampleTrick());
Debug.Log("Calling Trick 1");
trick1Once = false;
}
}
void Option2()
{
int energyCost = 20;
if (Energy - energyCost >= 0 && trick2Once)
{
doingTrick = true;
Energy -= energyCost;
Instantiate(trick2);
Debug.Log("Calling Trick 2");
StartCoroutine(ExampleTrick());
trick2Once = false;
}
}
private void Update()
{
if (!doingTrick && activeVillager)
{
option1B.gameObject.SetActive(true);
option2B.gameObject.SetActive(true);
}
else
{
option1B.gameObject.SetActive(false);
option2B.gameObject.SetActive(false);
}
happySlider.value = Happiness;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Villager"))
{
activeVillager = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Villager"))
{
activeVillager = false;
}
}
IEnumerator ExampleTrick()
{
energySlider.value = Energy * 0.01f;
yield return new WaitForSeconds(10);
doingTrick = false;
}
}
Comment