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 /
  • Help Room /
avatar image
1
Question by masteproofgamel · Jun 04, 2016 at 03:52 PM · c#script.characterrpgrpg-game

RPG Class Selection Menu

So I i'm making an RPG game and I have 3 classes; warrior, mage, assasin made by me following HardlyBrief Programming's tutorial. He shows how to do it using the GUI system, but I wanna use canvas. Can someone tell me the script for example: if mage class is select, player's class is mage? I know the logic but I don't know how to translate it into C# code. Pls help.

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

2 Replies

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

Answer by Mmmpies · Jun 04, 2016 at 06:48 PM

Create a canvas, add a panel as the back ground. Add another panel and scale it so it's big enough to show 1 class. Then add 6 Text items to the panel (that's the name, description and four attributes that Dan showed.

Drag this script onto the panel

 using UnityEngine.UI;
 using UnityEngine;
 using System.Collections;
 
 public class DisplayClass : MonoBehaviour {
 
     public enum CharType {aMage, aWarrior};    // just a list of names that make sense
 
     public CharType myType;        // an enum of CharType = this will show in the inspector
 
     private BaseCharacterClass myClass;        // a Base Class to be set based on myType
 
     public Text ClassName;                // Just a bunch of public text boxes to drag into the inspector
     public Text ClassDescription;
     public Text ClassStrength;
     public Text ClassEndurance;
     public Text ClassStamina;
     public Text ClassIntelligent;
 
 
 
     void Start()
     {
         if (myType == CharType.aMage)
             myClass = new BaseMageClass ();
         else if (myType == CharType.aWarrior)
             myClass = new BaseWarriorClass ();
         else
             Debug.LogError("I have no class at all!");
 
         ClassName.text = myClass.CharacterClassName;
         ClassDescription.text = myClass.CharacterClassDescription;
         ClassStrength.text = myClass.Strength.ToString ();
         ClassEndurance.text = myClass.Endurance.ToString ();
         ClassStamina.text = myClass.Stamina.ToString ();
         ClassIntelligent.text = myClass.Intelligence.ToString ();
     }
 }

(the panel with all the texts on it) then select that panel and look in the inspector. You should see a load of empty slots in the script attached to the panel so go ahead and drag each text UI item from the hierarchy onto the empty slots in turn.

Select which class type you want in the inspector. Click play and it should have filled in that class.

Now the good bit :D

Highlight that panel and duplicate it, drag it off to a spare bit of the canvas and from the enum in the inspector select the other class.

I only put 2 in and if you had loads you might want a case statement rather than as if else but with 3 I wouldn't worry about it.

Comment
Add comment · Show 5 · 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 masteproofgamel · Jun 05, 2016 at 03:15 PM 0
Share

But I need even something for making a class be selected by clicking on the character type image. So for example: There is an image of the warrior character and when it is selected the warrior class will be true.

avatar image Mmmpies masteproofgamel · Jun 05, 2016 at 03:35 PM 0
Share

Easy enough to do. Have your 3 images as the Image element of a button in the UI.

Change that script so ins$$anonymous$$d of Start() you have

 public void SetClass(int myTypeNo)
     {
         if (myTypeNo == 1)
             myClass = new Base$$anonymous$$ageClass ();
         else if (myTypeNo == 2)
             myClass = new BaseWarriorClass ();
         else
             Debug.LogError("I have no class at all!");

You can delete the enum stuff as we can just do this with an int (you only have 3 classes after all).

On each button click + in the onClick area (again a slot will appear) drag the panel with that script attached to it onto the slot.

From the drop down select DisplayClass -> SetClass and a little Int box appears. In my script I set $$anonymous$$age with 1 and Warrior with 2. So on the $$anonymous$$age button change that int to 1 and on the Warrior to 2.

That's all there is to it.

avatar image masteproofgamel · Jun 05, 2016 at 04:14 PM 0
Share

If in the first script you made I add 3 functions: SetWarriorClass(); Set$$anonymous$$ageClass(); SetAssassinClass();

For each function I set the private bool isWarriorClass, is$$anonymous$$ageClass, isAssassinClass wich I created before ins$$anonymous$$d of the enum, true. And I add the button component to the images and OnClick the function will be called?

For example if I click the Warrior icon I call the SeWarriorClass() function wich says the player's class is a new WarriorClass will it work?

avatar image Mmmpies masteproofgamel · Jun 05, 2016 at 04:23 PM 1
Share

You need to get your head round the event system.

$$anonymous$$ake your functions public then you click + in the OnClick of each button to make the slot appear. Drag the panel with your script attached to it onto that slot and from the DropDown select

YourScriptName -> YourFunctionName

and it'll call that function for that button. So yes it'll work IF you make that function PUBLIC (very important, hence the caps) and you setup the OnClick Event for each button.

avatar image masteproofgamel Mmmpies · Jun 05, 2016 at 04:36 PM 0
Share

Ok thank you for the big help!

avatar image
0

Answer by masteproofgamel · Jun 06, 2016 at 09:08 AM

Sry but now I have another problem: When I click the image the class changes but the stats no. So if I click the warrior image the private bool isWarriorClass that I made public changes but the stats don't.

Here's the code: using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class UI_Creazione_P : MonoBehaviour {
 
     private BasePlayer newPlayer;
     public bool isWarriorClass;
     public bool isMageClass;
     public bool isAssassinClass;
     public Image Guerriero;
     public Image Mago;
     public Image Assassino;
     public Text Nome_Classe;
     public Text Descrizione_Classe;
     public Text Forza;
     public Text Intelligenza;
     public Text Destrezza;
 
     void Start()
     {
         newPlayer = new BasePlayer();
 
         isWarriorClass = false;
         isMageClass = false;
         isAssassinClass = false;
 
 
         if (isWarriorClass == true)
         {
             newPlayer.ClasseGiocatore = new WarriorClass();   // ClasseGiocatore = CharacterClass
         }
         if (isMageClass == true)
         {
             newPlayer.ClasseGiocatore = new MageClass();
         }
         if (isAssassinClass == true)
         {
             newPlayer.ClasseGiocatore = new AssassinClass();
         }
         else
         {
             Debug.Log("NO CLASS");
         }
 
         PlayerStats();
     }
 
     public void SetWarriorClassTrue()
     {
         isMageClass = false;
         isAssassinClass = false;
         isWarriorClass = true;
     }
 
     public void SetMageClassTrue()
     {
         isMageClass = true;
         isWarriorClass = false;
         isAssassinClass = false;
     }
 
     public void SetAssassinClassTrue()
     {
         isAssassinClass = true;
         isMageClass = false;
         isWarriorClass = false;
     }
 
     void PlayerStats()
     {
         // Classe giocatore
 
         // Vita giocatore
         newPlayer.VitaAttuale = newPlayer.ClasseGiocatore.VitaAttuale;
         newPlayer.VitaMassima = newPlayer.ClasseGiocatore.VitaMassima;
         newPlayer.VitaMinima = newPlayer.ClasseGiocatore.VitaMinima;
 
         // Energia giocatore
         newPlayer.EnergiaAttuale = newPlayer.ClasseGiocatore.EnergiaAttuale;
         newPlayer.EnergiaMassima = newPlayer.ClasseGiocatore.EnergiaMassima;
         newPlayer.EnergiaMinima = newPlayer.ClasseGiocatore.EnergiaMinima;
 
         // Forza
         newPlayer.Forza = newPlayer.ClasseGiocatore.Forza;
 
         // Intelligenza
         newPlayer.Intelligenza = newPlayer.ClasseGiocatore.Intelligenza;
 
         // Destrezza
         newPlayer.Destrezza = newPlayer.ClasseGiocatore.Destrezza;
 
         Nome_Classe.text = newPlayer.ClasseGiocatore.Nome;
         Descrizione_Classe.text = newPlayer.ClasseGiocatore.Descrizione;
         Forza.text = newPlayer.Forza.ToString();
         Intelligenza.text = newPlayer.Intelligenza.ToString();
         Destrezza.text = newPlayer.Destrezza.ToString();
     }
 }
 
Comment
Add comment · Show 4 · 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 masteproofgamel · Jun 06, 2016 at 09:09 AM 0
Share

// forza = strength, // intelligenza = intellect, // destrezza = agility, nome_classe = class_name, descrizione_classe = class_description. ClasseGiocatore = CharacterClass

I wrote it a bit in italian and english sorry.

avatar image Mmmpies · Jun 06, 2016 at 09:16 AM 0
Share

Well you're setting the bool to true but you're not actually setting the class are you. Think about this.

Start()

runs once when the game starts, it doesn't get called (and it shouldn't get called - even by you) again.

If you want to stick to your way of calling the separate functions then add the set line to each function. Or, as it looks like that script is getting more structure to it (maybe in that tutorial it extends the features), you need to just show the stats and call that newPlayer base class when you click on an Accept button to set your player.

avatar image masteproofgamel Mmmpies · Jun 06, 2016 at 10:02 AM 0
Share

Got it work but I have still a stupid problem: When I click the warrior and assassin image the class name doesn't appear ins$$anonymous$$d, for the mage it works.

avatar image Mmmpies masteproofgamel · Jun 06, 2016 at 10:49 AM 0
Share

Well you'd be better off creating a separate function and calling that. No real need to call it every single frame (which Update does) it only needs to call when a button is clicked.

How you show your game is up to you but if you want to show the base class then you don't need to set the player up till you click a Create button.

If you just want them to click the one you choose and it just creates the the player then it's O$$anonymous$$ but I think you'll want to show the class information and then have a separate function to create.

So when your function gets called set the bools then call a ShowClass() function to grab that class's base information.

When you click the Create button actually set the player to be the currently selected base type in another function.

Hope this makes sense.

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

6 People are following this question.

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

Related Questions

Referencing instances of scripts based on the GameObject they are attached to. 0 Answers

Map creator inside my game 0 Answers

RPG instatiating the player 2 Answers

How do i put "Wasted!" after respawn? 0 Answers

Trying to make a door. Zoning or Load Level with Animation. 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