• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

I'm trying to sort my Npc's into different social classes 1 Answer

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

Disabling function of a key 1 Answer

Double subtraction issue 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges