- Home /
Using accessors and mutators from another class
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);
}
}
Answer by Garazbolg · Jan 25, 2016 at 02:23 PM
Not quite but you are close.
The first error is that you declare your field myStats in the start function so as soon as your thread leaves it you will have lost myStats. You need to declare it as a field of WorkerStats :
using UnityEngine;
using System.Collections;
public class WorkerStats : MonoBehaviour
{
public class Stats
{
/* your class code here */
}
public Stats myStats;
void Start()
{
myStats = new Stats("Benny",1,100,0);
}
}
Then you can access it the way you did.
Also be carefull, you're trying to assign a value to a getter :
ws.myStats.SetLevel(myStats.GetLevel() += 1);
It will work but i think it will get you a warning. Just use a simple '+'. Or you could use Unity built-in getter/setter system to do just :
ws.myStats.level += 1;
And still control what the user put in.
Hope it helps you understand more about Unity Scripting. I'm guessing you're coming from C++. Well it isn't much different but you should find some tutorials on the internet on how to script for unity, there is a ton of it.
Ah yes, that makes sense. I tinkered around a bit and managed to get it working using your advice, I'm also no longer trying to assign a value to the getter, mistake on my part there :P I shall look into Unitys' built in getter/setter system.
Thanks for the help @Garazbolg
Answer by coolraiman · Jan 25, 2016 at 03:55 PM
if you are already experienced with c#, the first thing to learn in unity is how to make script interact with each others
look at the object in your scene with the component (everything is a component)) IncreaseWorkerLevelk.
you should see in the inspector once the object is selected : Increase Worker Level and jsut under that, a public field named ws with a rectangle next to it. try to slide your object with the component Worker Stats in that rectangle. A reference is now made.
if you want to make a reference at runtime you can use in your start function or anywhere else:
ws = GameObject.Find("nameOfTheObjectWithTheWorkerStatsComponent").GetComponent();
from these basics, you should be able to play with how script interact each other
Your answer
![](https://koobas.hobune.stream/wayback/20220612061625im_/https://answers.unity.com/themes/thub/images/avi.jpg)