Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jazzds · Nov 09, 2016 at 06:31 PM · c#classespublic variable

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;
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jazzds · Nov 09, 2016 at 08:49 PM 0
Share

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.

avatar image ElijahShadbolt · Nov 10, 2016 at 08:47 PM 0
Share

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
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# | public variables in parent class should not show up in child class 1 Answer

Classes and Scripting 2 Answers

Need my function to work with different lists of different values (classes) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges