Accessing values on a gameobject
So I am fairly new to unity and programming I have 6 gameobjects called Upgrade1, Upgrade2 etc, and i want to access the count variable in their script for each individual one and manipulate that from another script
The Upgrade Manager Class
public void PurchasedUpgrade(){
if(click.gold >= cost) {
click.gold -= cost;
count += 1;
click.goldperclick += clickPower;
cost = Mathf.Round (baseCost * Mathf.Pow (1.25f, count));
}
}
and then i have the persistence script
public void Fred()
{
upgrademanager = GetComponent<UpgradeManager>();
and i need to do this to get the count value from each individual item so what should i do in the persistence script Sorry if this question is confusing i dont know too much about programming and unity. I have done a few tutorials though.
Answer by SohailBukhari · Mar 27, 2017 at 07:35 AM
Get Reference of Your class in the persistent script and access it count. Then You Can Assign it from the inspector or find with the type.
public class UpgradeManager
{
public GameObject[] _objects;
public int Count;
}
public class Persistent
{
private UpgradeManager _myClass;
private void Start()
{
_myClass = GameObject.FindObjectOfType<UpgradeManager>();
_myClass.Count = 5;
}
}
Your answer
Follow this Question
Related Questions
How can I access to child objects variables from other C# script ? 0 Answers
Multiple Floors: how to hide/show a floor when a character is climbing/descending ? 0 Answers
On Button Click Create a gameobject (ui,no mouse stuff) 1 Answer
On Button Click Create a gameobject (ui,no mouse stuff) 0 Answers
Best way to set up a player object 0 Answers