I’m trying to learn about classes and how they can be used within Unity, I have the following code for one of the objects in my scene:
using UnityEngine;
using System.Collections;
public class WorkerStats : MonoBehaviour
{
public class Stats
{
public string workerName;
public int level;
public int health;
public int resources;
public Stats()
{
}
public Stats(string nameParam, int levelParam, int healthParam, int resourcesParam)
{
workerName = nameParam;
level = levelParam;
health = healthParam;
resources = resourcesParam;
}
public void SetName(string nameParam)
{
workerName = nameParam;
}
public string GetName()
{
return workerName;
}
//I have get and set functions for all variables
}
void Start()
{
Stats myStats = new Stats();
myStats.SetLevel(1);
myStats.SetName("Benny");
myStats.SetHealth(100);
myStats.SetResources(0);
}
}
These will define the object that they are attached to, say I wanted to use one of these functions from another class, for example, increasing the workers level by one.
For instance, I can imagine it would be something like this, now I know this is wrong but would i be something alone these lines?
public class IncreaseWorkerLevel : MonoBehaviour
{
public WorkerStats ws;
void Start()
{
ws.myStats.SetLevel(myStats.GetLevel() += 1);
}
}