• 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 samaxi · Apr 02, 2014 at 04:24 PM · c#javascriptgui

What Am I Doing Wrong? Variable Names

I want my code to be, if Combatstyle = 1, than CombatStyleName = "Melee"

But I don't know what I am doing wrong with my code.

Please help.

Thank you.

     #pragma strict
     var skills = false;
     var abilities = false;
     var faith = false;
     var inventory = false;
     var equipment = false;
     var spells = false;
     
     static var CombatStyle = 0;
     var CombatStyleName = "";
     
     function Awake(){
     CombatStyle = 1;
     }
     function Update(){
     if (Input.GetKeyDown (KeyCode.Tab)){
     CombatStyle++;
     }
     if(CombatStyle == 5){
     CombatStyle = 1;
     }
     if(CombatStyle == 1){
     CombatStyleName = "Melee";
     }
     if(CombatStyle == 2){
     CombatStyleName = "Range";
     }
     if(CombatStyle == 3){
     CombatStyleName = "Holy Magic";
     }
     if(CombatStyle == 4){
     CombatStyleName = "Unholy Magic";
     }
     }
     
     function OnGUI () {
     if(GUI.Button (Rect (1140, 250, 75, 20), "Skills")){
     skills = true;
     abilities = false;
     faith = false;
     inventory = false;
     equipment = false;
     spells = false;
     }
     
     if(GUI.Button (Rect (1220, 250, 75, 20), "Abilities")){
     skills = false;
     abilities = true;
     faith = false;
     inventory = false;
     equipment = false;
     spells = false;
     }
     
     if(GUI.Button (Rect (1300, 250, 75, 20), "Faith")){
     skills = false;
     abilities = false;
     faith = true;
     inventory = false;
     equipment = false;
     spells = false;
     }
     
     if(GUI.Button (Rect (1140, 280, 75, 20), "Inventory")){
     skills = false;
     abilities = false;
     faith = false;
     inventory = true;
     equipment = false;
     spells = false;
     }
     
     if(GUI.Button (Rect (1220, 280, 75, 20), "Equipment")){
     skills = false;
     abilities = false;
     faith = false;
     inventory = false;
     equipment = true;
     spells = false;
     }
     
     if(GUI.Button (Rect (1300, 280, 75, 20), "Spells")){
     skills = false;
     abilities = false;
     faith = false;
     inventory = false;
     equipment = false;
     spells = true;
     }
     
     if(skills == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Skills!");
     
     GUI.Label (Rect (1160, 325, 100, 20), "Attack");
     GUI.Label (Rect (1150, 345, 1000, 20), "Current Level : " +Attack.curLevel);
     GUI.Label (Rect (1150, 365, 1000, 20), "Current EXP : " +Attack.curExp);
     GUI.Label (Rect (1150, 385, 1000, 20), "EXP to level : " +Attack.expLeft);
     
     GUI.Label (Rect (1160, 415, 100, 20), "Range");
     GUI.Label (Rect (1150, 435, 1000, 20), "Current Level : " +Range.curLevel);
     GUI.Label (Rect (1150, 455, 1000, 20), "Current EXP : " +Range.curExp);
     GUI.Label (Rect (1150, 475, 1000, 20), "EXP to level : " +Range.expLeft);
     
     GUI.Label (Rect (1160, 505, 100, 20), "Holy Mage");
     GUI.Label (Rect (1150, 525, 1000, 20), "Current Level : " +Holymage.curLevel);
     GUI.Label (Rect (1150, 545, 1000, 20), "Current EXP : " +Holymage.curExp);
     GUI.Label (Rect (1150, 565, 1000, 20), "EXP to level : " +Holymage.expLeft);
     
     GUI.Label (Rect (1160, 595, 100, 20), "Unholy Mage");
     GUI.Label (Rect (1150, 615, 1000, 20), "Current Level : " +Unholymage.curLevel);
     GUI.Label (Rect (1150, 635, 1000, 20), "Current EXP : " +Unholymage.curExp);
     GUI.Label (Rect (1150, 655, 1000, 20), "EXP to level : " +Unholymage.expLeft);
     
     GUI.Label (Rect (1150, 685, 100, 20), "Combat Style : " +CombatStyleName);
     }
     if(abilities == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Abilities!");
     }
     if(faith == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Faith!");
     }
     if(inventory == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Inventory!");
     }
     if(equipment == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Equipment!");
     }
     if(spells == true){
     GUI.Box (Rect (1140, 305, 230, 700), "Spells!");
     }
     }
Comment
Add comment · Show 5
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 Gruffy · Apr 02, 2014 at 04:39 PM 0
Share

Hey bud, I think your problem is here firstly...

 "Combat Style : " +combatStyle.CombatStyleName);

This bit above, according to your script`s name should be...

 "Combat Style : " +Combatstyle.CombatStyleName);
avatar image samaxi · Apr 02, 2014 at 04:42 PM 0
Share

I have the names right, its just the CombatStyleName isn't changing from "" to "$$anonymous$$elee"/"Range"/"Holy $$anonymous$$agic"/"Unholy $$anonymous$$agic"

avatar image Slobdell · Apr 02, 2014 at 04:44 PM 0
Share

Yeah that's what I was saying, note the capitalization differences between your code and Samaxis code

avatar image samaxi · Apr 02, 2014 at 04:55 PM 0
Share

I edited my script and it still doesn't work.

avatar image Lo0NuhtiK · Apr 02, 2014 at 04:57 PM 0
Share

http://en.wikipedia.org/wiki/Naming_convention_%28programming%29

http://unitygems.com/coding-conventions/

https://www.google.com/search?q=code+naming+convention

Just sayin.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Slobdell · Apr 02, 2014 at 04:42 PM

Assuming there are no typos, you're updating the static variable of Combatstyle. In your gui you are accessing combatStyle.combatStyleName which would be an instance variable. So either change your gui to show Combatstyle.combatStyleName OR you need to update the instance vars as well. Although I don't see any instance vars here so maybe that's a typo?

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 samaxi · Apr 02, 2014 at 04:56 PM

Okay I change around my code for my HUD

 #pragma strict
 var skills = false;
 var abilities = false;
 var faith = false;
 var inventory = false;
 var equipment = false;
 var spells = false;
 
 static var CombatStyle = 0;
 var CombatStyleName = "";
 
 function Awake(){
 CombatStyle = 1;
 }
 function Update(){
 if (Input.GetKeyDown (KeyCode.Tab)){
 CombatStyle++;
 }
 if(CombatStyle == 5){
 CombatStyle = 1;
 }
 if(CombatStyle == 1){
 CombatStyleName = "Melee";
 }
 if(CombatStyle == 2){
 CombatStyleName = "Range";
 }
 if(CombatStyle == 3){
 CombatStyleName = "Holy Magic";
 }
 if(CombatStyle == 4){
 CombatStyleName = "Unholy Magic";
 }
 }
 
 function OnGUI () {
 if(GUI.Button (Rect (1140, 250, 75, 20), "Skills")){
 skills = true;
 abilities = false;
 faith = false;
 inventory = false;
 equipment = false;
 spells = false;
 }
 
 if(GUI.Button (Rect (1220, 250, 75, 20), "Abilities")){
 skills = false;
 abilities = true;
 faith = false;
 inventory = false;
 equipment = false;
 spells = false;
 }
 
 if(GUI.Button (Rect (1300, 250, 75, 20), "Faith")){
 skills = false;
 abilities = false;
 faith = true;
 inventory = false;
 equipment = false;
 spells = false;
 }
 
 if(GUI.Button (Rect (1140, 280, 75, 20), "Inventory")){
 skills = false;
 abilities = false;
 faith = false;
 inventory = true;
 equipment = false;
 spells = false;
 }
 
 if(GUI.Button (Rect (1220, 280, 75, 20), "Equipment")){
 skills = false;
 abilities = false;
 faith = false;
 inventory = false;
 equipment = true;
 spells = false;
 }
 
 if(GUI.Button (Rect (1300, 280, 75, 20), "Spells")){
 skills = false;
 abilities = false;
 faith = false;
 inventory = false;
 equipment = false;
 spells = true;
 }
 
 if(skills == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Skills!");
 
 GUI.Label (Rect (1160, 325, 100, 20), "Attack");
 GUI.Label (Rect (1150, 345, 1000, 20), "Current Level : " +Attack.curLevel);
 GUI.Label (Rect (1150, 365, 1000, 20), "Current EXP : " +Attack.curExp);
 GUI.Label (Rect (1150, 385, 1000, 20), "EXP to level : " +Attack.expLeft);
 
 GUI.Label (Rect (1160, 415, 100, 20), "Range");
 GUI.Label (Rect (1150, 435, 1000, 20), "Current Level : " +Range.curLevel);
 GUI.Label (Rect (1150, 455, 1000, 20), "Current EXP : " +Range.curExp);
 GUI.Label (Rect (1150, 475, 1000, 20), "EXP to level : " +Range.expLeft);
 
 GUI.Label (Rect (1160, 505, 100, 20), "Holy Mage");
 GUI.Label (Rect (1150, 525, 1000, 20), "Current Level : " +Holymage.curLevel);
 GUI.Label (Rect (1150, 545, 1000, 20), "Current EXP : " +Holymage.curExp);
 GUI.Label (Rect (1150, 565, 1000, 20), "EXP to level : " +Holymage.expLeft);
 
 GUI.Label (Rect (1160, 595, 100, 20), "Unholy Mage");
 GUI.Label (Rect (1150, 615, 1000, 20), "Current Level : " +Unholymage.curLevel);
 GUI.Label (Rect (1150, 635, 1000, 20), "Current EXP : " +Unholymage.curExp);
 GUI.Label (Rect (1150, 655, 1000, 20), "EXP to level : " +Unholymage.expLeft);
 
 GUI.Label (Rect (1150, 685, 100, 20), "Combat Style : " +CombatStyleName);
 }
 if(abilities == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Abilities!");
 }
 if(faith == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Faith!");
 }
 if(inventory == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Inventory!");
 }
 if(equipment == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Equipment!");
 }
 if(spells == true){
 GUI.Box (Rect (1140, 305, 230, 700), "Spells!");
 }
 }
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 BadAssGames · Oct 13, 2014 at 12:55 PM

To help you with debugging in the future, you should use enumerators when you're setting up menu systems like this. I used to do this same thing when I started writing code, and it is a horrible practice. You have some 100 lines of code that could be cut down to like 30. Try this out:

 public Enum MenuPage { Skills, abilities, faith, inventory, equipment, spells}//These are all the possible menu pages
 public MenuPage curPage = MenuPage.skills;//Default to the skills page
 
 
 if(GUI.Button(rect, "Abilities"))
 {//Less variables to set means less possibility for errors
    curPage = MenuPage.Abilities;
 }
 
 
 if(curPage == MenuPage.skills)
 {//Only this check ensures that only one page is active at a time, so there is no need to worry about setting a bunch of bools
     DrawMenuPage();
 } 
 
 if(curPage == MenuPage.skills || curPage == menuPage.faith)
 {//But it can also be used to check multiples really easily
    DoSomething();
 }
 
 //This same principle can be applied to CombatStyle
 
 public Enum CombatStyle { Melee, Range, HolyMagic, UnholyMagic}
 public CombatStyle myStyle = CombatStyle.Melee;
 
 //The cool part is you can iterate through the list of CombatStyles really easily, because an enum is also a list of numbers
 
 if (Input.GetKeyDown (KeyCode.Tab))
 {
     CombatStyle = (int)CombatStyle ++;//I may have the syntax here wrong
 }
 
 //Then you can display the Combat Style with ToString()
 
 GUI.Label(rect, "My Combat Style " + myCombatStyle.ToString());

If you refactor the script using these concepts, I guarantee you'll fix your issue and prevent future issues from occurring.

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 MrSoad · Oct 13, 2014 at 01:13 PM 0
Share

Just a thought, you are using "#pragma strict" and not explicitly declaring your variable types. eg:

  var skills = false;


should be

 var skills : boolean = false;

Not your issue but something to consider doing in the future. Also you are using a "static" variable. I would strongly advise you to not use statics, much better to pass vars around rather than using these.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Randomly Generated Objects 1 Answer

CS0116 "A namespace can only contain type and namespace declarations" 1 Answer

Need help with my script 0 Answers

How to make a button script. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges