Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 WesterlyCarrot9 · Jan 26, 2014 at 05:19 AM · c#guilistclassrpg

C# List and GUI

Greetings! So i have the two scripts below and i was wondering how to go about making some GUI for them. For example, have a button called "Warrior" and when press the Warrior item from the Character.cs script along with its stats is loaded.

 RPGClass.cs

 using UnityEngine;
 using System.Collections;
 
 public class RPGClass : MonoBehaviour{
     public string ClassName;
     public int Strength;
     public int Vitality;
     public int Dexterity; 
     public int Intelligence;
 
     public RPGClass(string cName, int str, int vit, int dex, int intel){
         ClassName = cName;
         Strength = vit;
         Dexterity = dex;
         Intelligence = intel;
     }
 }

 Character.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Character : MonoBehaviour{
     void Start(){
         List<RPGClass> rpgclasses = new List<RPGClass>();
 
         rpgclasses.Add(new RPGClass("Warrior",12,12,10,8));
         rpgclasses.Add(new RPGClass("Archer",10,11,12,9));
     }
 }
Comment
Add comment · Show 1
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 Kiwasi · Jul 05, 2014 at 10:59 AM 0
Share

Heads up, you might have problems using new with $$anonymous$$onoBehaviours. Unity likes to use start as a constructor for them and will likely protest.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nesis · Jan 26, 2014 at 06:04 AM

I imagine the most general way to do it would do something tricky with Unity's class serialisation, but I've not dabbled much in that so I won't even begin to suggest how to do it. Tapping into Unity's class serialisation would possibly make this work for other scripts too...?

The less generalisable way I'd go about it is to add a method to RPGClass that generates some GUI stuff to show stats, and call it in an OnGUI() method in the Character class. The Character class would have a boolean flag to determine whether or not the GUI should be drawn.

RPGClass.cs

 using UnityEngine;
 using System.Collections;
  
 public class RPGClass : MonoBehaviour{
     public string ClassName;
     public int Strength;
     public int Vitality;
     public int Dexterity; 
     public int Intelligence;
  
     public RPGClass(string cName, int str, int vit, int dex, int intel){
        ClassName = cName;
        Strength = vit;
        Dexterity = dex;
        Intelligence = intel;
     }
     
     //A method to draw the RPGClass' GUI. Note that this 
     //will work only when called in OnGUI(), since it uses
     //GUI.Label()!
     public string DrawGUI() {
         GUI.Label(Rect(10f,10f,100f,20f),"Class: "+ClassName);
         GUI.Label(Rect(10f,30f,100f,20f),"Strength: "+Strength);
         GUI.Label(Rect(10f,50f,100f,20f),"Dexterity: "+Dexterity);
         GUI.Label(Rect(10f,70f,100f,20f),"Intelligence: "+Intelligence);
     }
 }


Character.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Character : MonoBehaviour{
     
     //if true, stats will be shown. if false, no stats will be shown.
     private bool showStats;
     
     //im guessing you want to store a list of RPGClasses... 
     //so you should make it a member variable by putting it here!
     List<RPGClass> rpgclasses = new List<RPGClass>();
     
     //set this to show the stats of that class when showing stats
     RPGClass currentClass;
     
     void Start(){
         rpgclasses.Add(new RPGClass("Warrior",12,12,10,8));
         rpgclasses.Add(new RPGClass("Archer",10,11,12,9));
         
         //initialise currentClass somehow... you can set this however you want
         SetCurrentClass(rpgclasses.Last());
     }
     
     //method to let you set currentClass from outside
     //this script if wanted
     public void SetCurrentClass(RPGClass rpgClass) {
         currentClass = rpgClass;
     }
     
     public void OnGUI () {
         //button to toggle showing stats
         if (GUI.Button(Rect(10,10,50,50),"Show Stats")) {
             showStats = !showStats;
         }
         
         //Draw stats on screen if showStats is true, and if currentClass isn't null.
         //Note this is less efficient than just enabling / disabling a GUIText, and
         //should ideally only be used if you're wanting the DrawGUI() method to 
         //create GUI elements that the user can interact with (buttons, text inputs, 
         //etc).
         if (showStats && currentClass) {
             currentClass.DrawGUI();
         }
     }
 }
Comment
Add comment · Show 1 · 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 WesterlyCarrot9 · Jan 26, 2014 at 06:14 AM 0
Share

Hello! Thanks for posting that! I tried to use it but i got several errors on both scripts :S Actually, i was thinking about making a third script that would be only the GUI and create boxes and button from there and somehow use the GetComponent to access the stats. Would that be possible?

avatar image
0

Answer by aceronn · Jul 05, 2014 at 09:36 AM

If you want to have stuff load like this, your best bet would be to instantiate a prefab. Make a prefab for each class (model, scripts, etc) and on button press, delete the last loaded prefab and instantiate a new one. I did this for models in the past so it should work similar for this project. Let me know if you need help. (ps sorry for late answer)

Comment
Add comment · 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
0

Answer by Kiwasi · Jul 05, 2014 at 10:58 AM

Here is how I would do it. I'm assuming you know how to get the appropriate references between scripts and declare variables and all that stuff. I'm also assuming you have appropriate code to load the player. The code just covers the GUI. This method is almost independent of the number of classes you have.

 void OnGUI () {
     foreach (RPGClass currentClass in rpgclasses){
         // You could also add other GUI stuff in here
         if(GUILayout.Button (currentClass.ClassName){
             // Create your player as required here
         }
     }
 }
Comment
Add comment · 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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't call derived class function from list of base class. Ideas? 1 Answer

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

How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers

Displaying the players through GUI 1 Answer

C# how to create a Descending GUI List 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