Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
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:

  1. 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
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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

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

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

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

Related Questions

How to turn off/on a collider using a bool variable from another script 1 Answer

If an integer ends with an 1. 1 Answer

Text based game C#, if key selected>open door,Text based game, using boolean to pick up key and open door 0 Answers

If statement automatically true 1 Answer

If/else statement is always else 1 Answer


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