Question by
Valderhaug · Oct 11, 2015 at 04:08 PM ·
booleanintif statementif else
Problems with debug and if-statement
I've got a set of one known issue here:
An if-statement expects a type to be boolean when it is an int. The peice of code works by itself, but not in combination with the entire code.
//Element int element; void Start() { if (element = 1) { Debug.Log("Type: Mundane"); } else if (element = 2) { Debug.Log("Type: Corrosive"); } else if (element = 3) { Debug.Log("Type: Explosive"); } else if (element = 4) { Debug.Log("Type: Electric"); } else { Debug.Log("Type: Cryogenic"); } }
Entire code:
using UnityEngine;
using System.Collections;
public class BalancedWeaponRandomization : MonoBehaviour
{
//Element
int element;
//Damage
float vDmg = 120; //base stat value
float rDmg = 0.175F; //range (+/-)
float dDmg = 0.175F; //max balance delta
public float resDmg; //result from calculations
//Fire Rate
float vFrt = 400;
float rFrt = 0.175F;
float dFrt = 0.175F;
public float resFrt;
//Range
float vRng = 800;
float rRng = 0.125F;
float dRng = 0.125F;
public float resRng;
//Resource cost
float vVal = 5000;
public float resVal;
void Update()
{
if (Input.GetKeyDown(KeyCode.KeypadEnter))
{
Debug.ClearDeveloperConsole();
GenerateWeapon();
}
}
void GenerateWeapon()
{
//Element
float k = Random.value;
if (k <= 4 / 16) { element = 1; }
else if (k <= 7 / 16) { element = 2; }
else if (k <= 10 / 16) { element = 3; }
else if (k <= 13 / 16) { element = 4; }
else { element = 5; }
//Pre calculation
float ranPre = (Random.value - 0.5F) * 2 + 1;
//Damage
float balDmg = -Random.value * ((ranPre - 1) / 1) * dDmg;
float ranDmg = (Random.value - 0.5F) * 2 * rDmg + 1;
resDmg = Mathf.Ceil((ranDmg + balDmg) * vDmg / 2) * 2;
//Fire Rate
float balFrt = -Random.value * ((ranDmg - 1 - balDmg) / rDmg) * dFrt;
float ranFrt = (Random.value - 0.5F) * 2 * rFrt + 1;
resFrt = Mathf.Ceil((ranFrt + balFrt) * vFrt / 10) * 10;
//Range
float balRng = -Random.value * ((ranFrt - 1 - balFrt) / rFrt) * dRng;
float ranRng = (Random.value - 0.5F) * 2 * rRng + 1;
resRng = Mathf.Ceil((ranRng + balRng) * vRng / 10) * 10;
//Resource cost
resVal = Mathf.Ceil(((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) + 1) * vVal / 10) * 10;
//Feedback stuff
if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= -(vDmg + vFrt + vRng)) { Debug.Log("Quality: Unparalleled"); }
else if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= -(vDmg + vFrt + vRng) * 0.6) { Debug.Log("Quality: Atrocious"); }
else if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= -(vDmg + vFrt + vRng) * 0.2) { Debug.Log("Quality: Inferior"); }
else if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= (vDmg + vFrt + vRng) * 0.2) { Debug.Log("Quality: Adequate"); }
else if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= (vDmg + vFrt + vRng) * 0.6) { Debug.Log("Quality: Superior"); }
else if ((resDmg / vDmg - 1) + (resFrt / vFrt - 1) + (resRng / vRng - 1) <= (vDmg + vFrt + vRng)) { Debug.Log("Quality: Exquisite"); }
else { Debug.Log("Quality: Unparalleled"); }
//Here it expects the int 'element' to be a boolean for some reason.
if (element = 1) { Debug.Log("Type: Mundane"); }
else if (element = 2) { Debug.Log("Type: Corrosive"); }
else if (element = 3) { Debug.Log("Type: Explosive"); }
else if (element = 4) { Debug.Log("Type: Electric"); }
else { Debug.Log("Type: Cryogenic"); }
Debug.Log("Damage: " + resDmg);
Debug.Log("Rate: " + resFrt);
Debug.Log("Range: " + resRng);
Debug.Log("Value: " + resVal);
}
}
Comment
Best Answer
Answer by Denvery · Oct 11, 2015 at 05:55 PM
Hello! Please write element == 1
etc. and all code will work.
"element = 1" means that we put "1" to variable "element". And this expression has type of Int. But "element == 1" means that we compare value of variable "element" and "1". And this expression has type of bool, as we need inside If operator