- Home /
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
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)