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 unfixedacorn · Jul 12, 2020 at 07:54 AM · if-statementsif statementif-statement

If statement not working

I was making a game mod, and needed a random number. It worked fine, but after some time, it broke. I did some debugging, and found that the error lied in the if statement that I used at line 38. How do I fix this?

You do not need to worry about commands starting with KM. That is not relevant to this problem.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 using KModkit;
 
 public class colorNumberCode : MonoBehaviour 
 {
     public KMBombInfo bomb;
     public KMAudio audio;
     
     public KMSelectable button1;
     public KMSelectable button2;
     public KMSelectable button3;
     public KMSelectable button4;
     
     public Material[] colorOptions;
     public Renderer led;
     private int ledIndex = 0;
     
     static int moduleIdCounter = 1;
     int moduleId;
     private bool colorPicked = false;
     private bool moduleSolved; 
     
     void Awake()
     {
         moduleId = moduleIdCounter++;
         button1.OnInteract += delegate () { PressButton1(); return false; };
         button2.OnInteract += delegate () { PressButton2(); return false; };
         button3.OnInteract += delegate () { PressButton3(); return false; };
         button4.OnInteract += delegate () { PressButton4(); return false; };
     }
 
     void Start() 
     {
         if(colorPicked == true)
         {
             Debug.Log("Call 1");
             PickLEDColor();
             colorPicked = true;
         }
     }
     
     void PickLEDColor()
     {
         Debug.Log("Call 2");
         ledIndex = UnityEngine.Random.Range(0,4);
         led.material = colorOptions[ledIndex];
         Debug.LogFormat("[Color Numbers #{0}] The color of the LED is {1}", moduleId, colorOptions[ledIndex].name);
     }
     
     void PressButton1 ()
     {
         Debug.LogFormat("[Color Numbers #{0}] You pushed button one", moduleId);
         //Execute if the LED is red
         if(ledIndex == 0)
         {
             Debug.LogFormat("[Color Numbers #{0}] That is correct, module solved", moduleId);
             moduleSolved = true;
             GetComponent<KMBombModule>().HandlePass();
         }
         //If it's not red, execute this
         else
         {
             Debug.LogFormat("[Color Numbers #{0}] That is wrong, strike", moduleId);
         }
     }
     
     void PressButton2 ()
     {
         Debug.LogFormat("[Color Numbers #{0}] You pushed button two", moduleId);
         //Execute if the LED is blue
         if(ledIndex == 1)
         {
             Debug.LogFormat("[Color Numbers #{0}] That is correct, module solved", moduleId);
             moduleSolved = true;
             GetComponent<KMBombModule>().HandlePass();
         }
         //If it's not blue, execute this
         else
         {
             Debug.LogFormat("[Color Numbers #{0}] That is wrong, strike", moduleId);
         }
     }
     
     void PressButton3 ()
     {
         Debug.LogFormat("[Color Numbers #{0}] You pushed button three", moduleId);
         //Execute if the LED is green
         if(ledIndex == 2)
         {
             Debug.LogFormat("[Color Numbers #{0}] That is correct, module solved", moduleId);
             moduleSolved = true;
             GetComponent<KMBombModule>().HandlePass();
         }
         //If it's not green, execute this
         else
         {
             Debug.LogFormat("[Color Numbers #{0}] That is wrong, strike", moduleId);
         }
     }
     
     void PressButton4 ()
     {
         Debug.LogFormat("[Color Numbers #{0}] You pushed button four", moduleId);
         //Execute if the LED is yellow
         if(ledIndex == 3)
         {
             Debug.LogFormat("[Color Numbers #{0}] That is correct, module solved", moduleId);
             moduleSolved = true;
             GetComponent<KMBombModule>().HandlePass();
         }
         //If it's not yellow, execute this
         else
         {
             Debug.LogFormat("[Color Numbers #{0}] That is wrong, strike", moduleId);
         }
     }
     
 }
 
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
2
Best Answer

Answer by Elango · Jul 12, 2020 at 09:59 AM

This check doing absolutely nothing

 if(colorPicked == true)

Default value set to false so this condition will never be met. Since it's called only from Start it will always fail. "colorPicker" seems like redundancy. You never use it and it's private so you won't be able to use it outside this class. You can completely remove this check and call PickLEDColor() right away. If you really need it then just change it to

 if(colorPicked == false)

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 unfixedacorn · Jul 12, 2020 at 03:21 PM 0
Share

Wow I feel dumb

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

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

Related Questions

Can I Put a IF In a IF?!? 2 Answers

Disabling function of a key 1 Answer

if statement only ever triggering the first condition even if it's false and the second one is true 0 Answers

Double subtraction issue 1 Answer

I'm having trouble setting a bool. 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