Jazzds
1
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;
}
}
}
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);
}
}