- Home /
Help With C# Debug
I recently Switched over from using Java script to using c# don't ask why i have my reasons but i am having a litle trouble debugging the main script of my game. I have tryed debugging it using the scripting refrence of unity(Most Helpful thing in this world ofther then google :D) but i still havent been able to figure out the way to properly do some of the statements.
public class Main : MonoBehaviour {
int maxHealth = 100;
double healthregen = 1;
int maxmana = 150; //Max amount of mana
double manaregenrate = 1;
double manaregen = manaregenrate;
int manabarLeft = 640;
int healthbarleft = 640;
int manabarverticle = 590;
int healthbarvarticle = 620;
int healthbarlength = 400;
int manabarlength = 400;
int WaitForBeforManaRegen = 2;
int WaitForBeforHealthRegen = 2;
int RegenHealthEvery = 1.0f;
int RegenManaEvery = 5.0f;
int AllowNegitiveMana = 0; //0 for false 1 for True
int AllowMoreThenMaxHealth = 0; //0 for false 1 for True
int AllowMoreThenMaxMana = 0; //0 for false 1 for True
static int Strength = 0;
static int Experiance= 0;
static int Damage = 25;
static int Level = 0;
static double Intel = 0;
static double Stamina = 0;
static double Wisdom = 1;
static int Balance = 0;
static int Income = 10;
static int Expense = 50;
static int Price = 0;
static int Fee = 0;
static int WithDrawlAmmount = 0;
static int MinDeposit = 5;
static int XpTradeAmmount = 0;
int StartingBalance = 1000;
//All of the leveling system works
int XpToLevel2 = 5;
int XpToLevel3 = 10;
int XpToLevel4 = 20;
int XpToLevel5 = 30;
int XpToLevel6 = 40;
int XpToLevel7 = 50;
int XpToLevel8 = 60;
int XpToLevel9 = 70;
int XpToLevel10 = 80;
int XpToLevel11 = 100;
int XpToLevel12 = 120;
int XpToLevel13 = 140;
int XpToLevel14 = 160;
int XpToLevel15 = 220;
int XpToLevel16 = 260;
int XpToLevel17 = 300;
int XpToLevel18 = 350;
int XpToLevel19 = 400;
int XpToLevel20 = 450;
int XpToLevel21 = 500;
int XpToLevel22 = 550;
int XpToLevel23 = 600;
int XpToLevel24 = 650;
int XpToLevel25 = 700;
int XpToLevel26 = 750;
int XpToLevel27 = 800;
int XpToLevel28 = 850;
int XpToLevel29 = 900;
int XpToLevel30 = 1000;
int XpToLevelElite = 1500;
void Start (){
healthRegen();
InvokeRepeating("BillPay", 0, 3600);
InvokeRepeating("Paycheck", 0, 3600);
InvokeRepeating("manaRegen", WaitForBeforManaRegen, RegenManaEvery );
InvokeRepeating("healthRegen", WaitForBeforHealthRegen, RegenHealthEvery);
HideGUIText();
//....
ApplyStats();
}
void Update (){
//This Stuff dosen't work
if(Input.GetKeyDown("1")) {
Experiance += 10;
Screen.showCursor = false;
}
if(Input.GetKeyDown("e")) {
if(curmana < 20){
Debug.Log("Not Enogh Mana");
ShowGUIText();
}
if (curmana > 20){
curHealth -= 10;
curmana -= 20;
}
}
if (AllowNegitiveMana < 1){
if(curmana < 0 ) {
curmana = 0;
}
}
//
//LEVEL SYSTEM STARTS HERE
//
if (Experiance > 1) {
if (Experiance > XpToLevel2) {
Level = 2;
Strength = 2;
Intel = 2;
}
if (Experiance > XpToLevel3) {
Level =3;
Strength = 3;
Intel =3;
//....
}
if (Experiance > XpToLevel4) {
Level =4;
Strength =4;
Intel =4;
//....
}
if (Experiance > XpToLevel5) {
Level =5;
Strength =5;
Intel =5;
//....
}
if (Experiance > XpToLevel6) {
Level =6;
Strength =6;
Intel =6;
//....
}
if (Experiance > XpToLevel7) {
Level =7;
Strength =7;
Intel =7;
//....
}
if (Experiance > XpToLevel8) {
Level =8;
Strength =8;
Intel =8;
//....
}
if (Experiance > XpToLevel9) {
Level =9;
Strength =9;
Intel =9;
//....
}
if (Experiance > XpToLevel10) {
Level =10;
Strength =10;
Intel =10;
//....
}
if (Experiance > XpToLevel11) {
Level =11;
Strength =11;
Intel =11;
//....
}
if (Experiance > XpToLevel12) {
Level =12;
Strength =12;
Intel =12;
//....
}
if (Experiance > XpToLevel13) {
Level =13;
Strength =13;
Intel =13;
//....
}
if (Experiance > XpToLevel14) {
Level =14;
Strength =14;
Intel =15;
//....
}
if (Experiance > XpToLevel15) {
Level =15;
Strength =15;
Intel =16;
//....
}
if (Experiance > XpToLevel16) {
Level =16;
Strength =16;
Intel =18;
//....
}
if (Experiance > XpToLevel17) {
Level =17;
Strength =18;
Intel =20;
//....
}
if (Experiance > XpToLevel18) {
Level =18;
Strength =20;
Intel =22;
//....
}
if (Experiance > XpToLevel19) {
Level =19;
Strength =22;
Intel =24;
//....
}
if (Experiance > XpToLevel20) {
Level =20;
Strength =24;
Wisdom =2;
Intel =26;
//....
}
if (Experiance > XpToLevel21) {
Level =21;
Strength =30;
Wisdom =3;
Intel =32;
//....
}
if (Experiance > XpToLevel22) {
Level =22;
Strength =32;
Intel =45;
//....
}
if (Experiance > XpToLevel23) {
Level =23;
Strength =35;
Intel =50;
//....
}
if (Experiance > XpToLevel24) {
Level =24;
Strength =38;
Intel =55;
//....
}
if (Experiance > XpToLevel25) {
Level =25;
Strength =40;
Wisdom =5;
//....
}
if (Experiance > XpToLevel26) {
Level =26;
Strength =42;
//....
}
if (Experiance > XpToLevel27) {
Level =27;
Strength =43;
//....
}
if (Experiance > XpToLevel28) {
Level =28;
Strength =45;
//....
}
if (Experiance > XpToLevel29) {
Level =29;
Strength =50;
//....
}
if (Experiance > XpToLevel30) {
Level =30;
Strength =55;
Wisdom =6;
//....
}
if (Experiance > XpToLevelElite) {
Level = 1000;
Strength =100;
Wisdom =100;
Intel =500;
//....
}
//LEVEL SYSTEM ENDS HERE!
}
}
//This Has Errors
if (AllowMoreThenMaxHealth < 1){
if(curmana > 150) {
curmana = 150;
}
}
//
//KEY CODE LISTENERS START HERE
//
//These Dont Work
if(Input.GetKeyDown("1")) {
Experiance += 10;
}
if(Input.GetKeyDown("4"))
{
CharacterMotor.maxForwardSpeed +=15;
CharacterMotor.maxGroundAcceleration +=15;
Debug.Log("Checked");
}
if(Input.GetKeyDown("2")) {
curmana -= 10;
}
if(Input.GetKeyDown("3")) {
curmana -= 20;
}
if(Input.GetKeyDown("4")) {
curmana -= 10;
}
if(Input.GetKeyDown("5")) {
curmana -= 15;
}
//KEY CODE LISTENERS END HERE!
//
//Anit-Negitave Health Starts HERE!
//
if(curHealth < 0 ) {
curHealth = 0;
}
if (AllowMoreThenMaxHealth < 1){
if(curHealth > 100) {
curHealth = 100;
}
//ANIT-NEGITAVE HEALTH ENDS HERE!
}
//This Is for testing the Health and Mana system
//
//REGEN ITEMS START HERE!
//
void manaRegen (){
if (curmana < 150){
curmana += manaregen; //Add 5 mana to the curent mana
}
}
void healthRegen (){
if(curHealth < maxHealth) {
curHealth+=healthregen;
}
//REGEN ITEMS END HERE!
}
//
// GUI STUFF STARTS HERE!
//
void OnGUI (){
HealthsBar = GUI.HorizontalScrollbar( new Rect(Screen.width - 358,Screen.height - 50,350,50), 0, curHealth,0, maxHealth);
ManaBar = GUI.HorizontalScrollbar( new Rect(Screen.width - 358,Screen.height - 70,350,900), 0, curmana,0, maxmana);
GUI.Label ( new Rect(Screen.width - 200,Screen.height - 73,300,900), "Mana");
GUI.Label ( new Rect(Screen.width - 200,Screen.height - 53,350,50), "Health");
LevelText.text = "Level: " + Level + " " + "XP: " + Experiance;
}
void ShowGUIText (){
NoManaText.enabled = true;
StopCoroutine("HideGUIText");
StartCoroutine("HideGUIText");
}
void HideGUIText (){
yield return new WaitForSeconds(1.3f);
NoManaText.guiText.enabled = false;
}
void NotEnoghCashShowGUIText (){
NoCashText.enabled = true;
StopCoroutine("NotEnoghCashHideGUIText");
StartCoroutine("NotEnoghCashHideGUIText");
}
void NotEnoghCashHideGUIText (){
yield return new WaitForSeconds(1.0f);
NoCashText.guiText.enabled = false;
}
void LevelGUIText (){
LevelUpText.enabled = true;
yield return new WaitForSeconds(1.0f);
LevelUpText.guiText.enabled = false;
}
//GUI STUFF ENDS HERE!
//STAT SYSTEM STARTS HERE!
void ApplyStats (){
maxmana +=Intel;
maxHealth +=Stamina;
maxHealth +=Wisdom;
maxmana +=Wisdom;
Damage +=Strength;
Damage +=Wisdom;
manaregenrate += Intel;
}
//STAT SYSTEM ENDS HERE!
//
//Economy System Starts Here!
//
void Paycheck (){
Balance += Income;
}
void BillPay (){
if (Balance > Expense){
Balance -= Expense;
}
if (Balance < Expense) {
NotEnoghCashShowGUIText();
}
}
void Purchase (){
if (Balance > Price){
Balance -= Price;
}
}
void FeeSytem (){
if (Balance > Fee){
Balance -= Fee;
}
}
void Bal (){
Balance = Balance;
}
void withdrawl (){
if (Balance > WithDrawlAmmount){
Balance -= WithDrawlAmmount;
}
}
void MakeAccount (){
if (Balance > MinDeposit){
Balance -= MinDeposit;
}
}
void XpToCash (){
if (Experiance > XpTradeAmmount){
Experiance -= XpTradeAmmount;
Balance += XpTradeAmmount;
XpTradeAmmount = XpTradeAmmount;
Experiance = Experiance;
}
if (Experiance < XpTradeAmmount){
Debug.Log("No XP");
}
}
}
Please Explain Any Answer posted I am here to learn not for a free script.....
wow, you've got a lot going on...
What do you mean it's not working or going wrong? Are there actual errors or not doing what you expect? Also, why are you using static vars?
it has 12 Errors I cant seam to solve. all so they where static because I don't know any other way to acsess them with a function / void of a another script.
you'd be helping people help YOU if you listed those errors... as perchik suggested.
Gosh... Umh you might try replacing the Input.Get$$anonymous$$eyDown("1") parts with: Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.alpha1). I don't know if this will solve some of your errors, but it's worth a try?
Answer by Radivarig · Apr 09, 2014 at 11:36 AM
Here are some tips from me:
To be able to access your variables (or call functions) from another function make them public like
public int maxHealth = 100;
public void ManaRegen(){//..}
You should always write letter 'f' after float and 'd' after double and nothing after int types because in the conversion data can be rounded wrong, and float is more than enough for what you need so don't use double
public float manaRegenerate = 1f;
For true and false only variables use bool
public bool allowNegativeMana = false;
if(allowNegativeMana == false){//do something;}
Name your functions and classes with uppercase first letter and variables with lowercase first letters and each next different word with uppercase, that way you know what is a class and what is a variable and can now do this without confusion
public MyCustomClass myCustomClass = new MyCustomClass();
Your class name should always be equal to the .cs file name, that way you can add your scripts to GameObjects
Make separate functions that will do parts of your code like adding a mathematical model for your experience needed to level and make it as a return value of a function
public int XpForLevel(int level){
return level * someOther + expressionsYouDefine;
}
Instead of declaring each store some variables like xpToLevel in a List, (Dictionaries are also very useful)
public List<int> xpForLevel = new List<int>();
for (int i = 0; i < maxLevel; ++i){
xpForLevel.Add(XpForLevel(i) );
}
or manually
xpForLevel.Add(5);
xpForLevel.Add(10);
.
.
now you can access to for example level 5 like xpForLevel[4] (actual level minus 1 because that is how index access works, first element is at place 0), or change like this
xpForLevel[4] = 30;
Put simmilar variables in a separate class
public class CharacterStats : MonoBehavior{
public int strength = 0;
public int experience = 0;
//.
//.
public CharacterStats(){} //this is called a constructor for your class, it takes no arguments
}
save that class to a separate .cs file and then from Main do this
public CharacterStats charStats = new CharacterStats();
acces them
if(learnedSomething) charStats.experience += 100;
Letters pressed are recognized as
if(Input.GetKeyDown(KeyCode.A) == true){//do something;}
and numbers pressed as
if(Input.GetKeyDown(KeyCode.Alpha0) == true){//do something;};
Write as many functions as you need or can to help you with repetitive tasks,
public void LevelUp(int level)
{
if(level == 30)
return;
strength = level * yourExpression;
//.
//.
}
I would strongly suggest that you take some time and rewrite your whole project part by part, test those parts separately until you see them work and then combine them because when your project grown it gets harder to keep track of things and solve bugs, so it is better to prevent some bugs by taking more time for writing the code correctly. Also, the bugs can be any, because the compiler stopped on the first logical error or the previous bug generated others.
Good luck,
Radivarig
Answer by Angryboy · Apr 09, 2014 at 10:08 AM
I'm going to be using the layout from VisualStudio. Its more comfortable to read, though I guess that is personal opinion. All characters should be more-or-less the exact same, whitespaces not included.
Should beif(Input.GetKeyDown("e"))
if (AllowMoreThenMaxHealth < 1){if(Input.GetKeyDown(KeyCode.e))
//This Has Errors
if(curmana > 150) {
curmana = 150;
} } The error is that this is not in a method. Also my example will be in the VisualStudio format, MonoDeveloper will change the position of { } for some annoying reason. void MethodName() { if (AllowMoreThenMaxHealth < 1) { if (curmana > 150) { curmana = 150; } } } You cannot have code like this directly in the class-space. Only global variables (Stuff like maxHealth, maxmana, RegenHealthEvery, etc etc) can be in class-space without causing errors. Code must be in a method. The errors at "KEY CODE LISTENERS START HERE" are the same, they must be in a method. My editing of the code has caused issues, so I'm not sure if they are supposed to be in the same method as the "if allow_more_than_max_heatlh" "if cur_mana > 150" statements. Your call there. Final errors I've found are in GUI.Label ( new Rect(Screen.width - 200,Screen.height - 73,300,900), "Mana"); Apparently Rect isn't usable in that space, not sure why though. Perhaps a friendly coder could come along and explain this one for you?
Example of a getter/setter methods from my own game.
// Getter for $$anonymous$$aximum $$anonymous$$ana
public int Get_$$anonymous$$ax$$anonymous$$ana()
{
return $$anonymous$$ax_$$anonymous$$ana;
}
// Setter for $$anonymous$$aximum $$anonymous$$ana
public void Set_$$anonymous$$ax$$anonymous$$ana(int New$$anonymous$$ax)
{
$$anonymous$$ax_$$anonymous$$ana = New$$anonymous$$ax;
}
Calling the getter will return an int value with the current "max_mana" value. You can use it in a condition or a mathematical operation $$anonymous$$G.
// Condition will be max_mana greater than 10
// Lets say, a spell costs 10 mana, does player have enough mana to cast?
if ( Get_$$anonymous$$ax_$$anonymous$$ana() > 10)
{
// code for spell
}
A setter will allow you to change the value of a variable $$anonymous$$G. // Call setter-method Set_$$anonymous$$ax_$$anonymous$$ana(50); This method-caller can be used at any point, much like the getter. It will not return a value, and the "50" can be replaced with any valid value (In this case, it must be an integer).
The method for the setter basically takes this input value and use it as a variable identified as "New$$anonymous$$ax". Any point within the method that New$$anonymous$$ax is used, it will use the inputted value.
This is just a quick question: why don't you use the quick get/setters? like
public int $$anonymous$$ax$$anonymous$$ana()
{
get { return $$anonymous$$ax_$$anonymous$$ana; }
set { $$anonymous$$ax_$$anonymous$$ana = value; }
}
Either works, I just find it more convenient if you've got different methods for different things though.
slightly OT: @Angryboy - what do you mean by "more convenient if you've got different methods for different things though."?
get/set can perform all sorts of additional calculation/validation, so creating get/set methods seems to be more work. and if you don't need anything but the simplest get/set then why not something like:
public int $$anonymous$$ax$$anonymous$$ana {get; set;}
obviously, if it works for you, then you don't need to change anything but giving advice should not encourage anything that isn't a best-practice.
i won't even start on your na$$anonymous$$g conventions ;)
@Angryboy Getters and setters should be just that, getters and setters, not functions with a prepending "Get" or "Set". It's called convention.
Were not writing Java here, you don't need a get/set function for every variable.
Also, please don't put any "_" in your variable names, unless it's a private variable or function, in which case you can prepend one. Everything should follow upper or lower CamelCase.
Different languages have different conventions. You can't go use other conventions from other languages or make up your own unless you never plan to share your code ever. As a programmer, it's equally important to write good code as it is to follow standard conventions ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flick marble 0 Answers
How to make AI wonder about? C#? 2 Answers
Animation Scripting Help #C 1 Answer