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 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/Na$$anonymous$$g_convention_%28program$$anonymous$$g%29

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

https://www.google.com/search?q=code+na$$anonymous$$g+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.

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

Help| Convert a javascript to C# 1 Answer

Need help with my script 0 Answers

How to make a button script. 1 Answer

What is frame, How OnGuI is called every frame? 2 Answers


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