- Home /
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!");
}
}
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);
I have the names right, its just the CombatStyleName isn't changing from "" to "$$anonymous$$elee"/"Range"/"Holy $$anonymous$$agic"/"Unholy $$anonymous$$agic"
Yeah that's what I was saying, note the capitalization differences between your code and Samaxis code
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?
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!");
}
}
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.
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
Follow this Question
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