- Home /
Accessing class variables from a definition
So I made a template Job class for my game and made a new one for with all the details that were needed. I can access the class but I can't access the variables in the Samurai class at all and trying to make them public brings up errors. I was wondering if I needed to change the template or how to set up the Samurai class in the first place.
public class Classes : MonoBehaviour {
public class Job {
public int jobID;
public string name;
public string shortDesc;
public string longDesc;
public int attPlus;
public int magPlus;
public int abiPlus;
public int spdPlus;
public int lckPlus;
public int defPlus;
public int mdfPlus;
public int hpPlus;
}
public class Samurai : Job {
public void Main () {
Job samurai = new Job ();
samurai.jobID = 1;
samurai.name = "Samurai";
samurai.shortDesc = "A warrior with high speed and ability with a katana.";
samurai.attPlus = 1;
samurai.magPlus = 0;
samurai.abiPlus = 3;
samurai.spdPlus = 5;
samurai.lckPlus = 3;
samurai.defPlus = 0;
samurai.mdfPlus = 0;
samurai.hpPlus = 4;
}
}
}
Answer by ElijahShadbolt · Nov 09, 2016 at 08:02 PM
A class can have a constructor, in which you specify the values of the fields in that particular instance of the class object. For example:
public class Temp {
public int field;
// constructor
public Temp(int field) {
this.field = field;
}
}
// create new Temp object with field set to 2
public Temp tempObj = new Temp(2);
You can do this with your Job class, like so:
public class Classes : MonoBehaviour {
[System.Serializable] // display values in inspector
public class Job {
public int jobID;
public string name;
public string shortDesc;
public string longDesc;
public int attPlus;
public int magPlus;
public int abiPlus;
public int spdPlus;
public int lckPlus;
public int defPlus;
public int mdfPlus;
public int hpPlus;
// constructors
public Job() { } // empty constructor, use default values
public Job(int jobID, string name, string shortDesc, string longDesc,
int attPlus, int magPlus, int abiPlus, int spdPlus, int lckPlus, int defPlus, int mdfPlus, int hpPlus)
{
this.jobID = jobID;
this.name = name;
this.shortDesc = shortDesc;
this.longDesc = longDesc;
this.attPlus = attPlus;
this.magPlus = magPlus;
this.abiPlus = abiPlus;
this.spdPlus = spdPlus;
this.lckPlus = lckPlus;
this.defPlus = defPlus;
this.mdfPlus = mdfPlus;
this.hpPlus = hpPlus;
}
}
// create new Job object
public Job Samurai = new Job(
1, "Samurai", "A warrior with high speed and ability with a katana.", "",
1, 0, 3, 5, 3, 0, 0, 4);
}
Or during runtime, you can change the values of a Job object.
class OtherScript : MonoBehaviour
{
void Start()
{
Job samurai = new Job();
samurai.jobID = 1;
samurai.name = "Samurai";
samurai.shortDesc = "A warrior with high speed and ability with a katana.";
samurai.attPlus = 1;
samurai.magPlus = 0;
samurai.abiPlus = 3;
samurai.spdPlus = 5;
samurai.lckPlus = 3;
samurai.defPlus = 0;
samurai.mdfPlus = 0;
samurai.hpPlus = 4;
Debug.Log("Samurai job description is: " + samurai.shortDesc);
}
}
Would this still work with something that was like a character creation screen? $$anonymous$$y ideal is to just have a list of all the classes and when you select to be that, it gives you the values and you can read about it, apply to your character, etc.
Yes, it would work. Let's say you have an array of jobs. You can loop through each one and display information about the unique job.
Job[] jobs = new Job[]{
new Job(101, "Samurai", ...),
new Job(102, "President", ...),
new Job(103, "Advisor", ...)
};
foreach (Job job in jobs) {
Debug.Log(job.name);
// display job details
}
for (int i = 0; i < jobs.Length; ++i) {
Job job = jobs[i];
Debug.Log(job.name);
// display job details
}