- Home /
Else if not getting executed,Else if statement not executing
The if statement gets executed, however, the else if never does, even if the order isn't correct. i've tried it with adding an else path with a do nothing comment but nothing happens.
Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events;
public class CounterTrigger : MonoBehaviour {
public int maxCounter;
public bool ordered;
public UnityEvent onCounterEnd;
public int counter;
// Use this for initialization
void Start ()
{
counter = 0;
Debug.Log("beginning " + counter);
}
public void TriggerCounter(int i)
{
if (ordered && counter == i) {
counter++;
Debug.Log ("correct " + counter);
}
else if (!ordered)
{
counter = 0;
Debug.Log ("incorrect " + counter);
}
}
// Update is called once per frame
void Update ()
{
if (counter==maxCounter)
{
onCounterEnd.Invoke ();
}
}
} ,This is the code for the counter that should be increasing every time a correct button is clicked (order is given). However, when a wrong button is clicked the counter should reset to 0. Unfortunately, that statement never gets executed.
Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events;
public class CounterTrigger : MonoBehaviour {
public int maxCounter;
public bool ordered;
public UnityEvent onCounterEnd;
public int counter;
// Use this for initialization
void Start ()
{
counter = 0;
Debug.Log("beginning " + counter);
}
public void TriggerCounter(int i)
{
if (ordered && counter == i) {
counter++;
Debug.Log ("correct " + counter);
}
else if (!ordered)
{
counter = 0;
Debug.Log ("incorrect " + counter);
}
}
// Update is called once per frame
void Update ()
{
if (counter==maxCounter)
{
onCounterEnd.Invoke ();
}
}
}
Answer by bakir-omarov · Jun 11, 2018 at 08:15 AM
This Else If statement can be read and executed if your "ordered" bool will be false. In this script i can't see any line about "ordered". It is nothing about "counter", it is all about "ordered".
When I do it like this public void TriggerCounter(int i) { if (ordered == true && counter == i) { counter++; Debug.Log (counter); } else if (ordered == false) { counter = 0; Debug.Log (counter); } }
it still doesn't get executed.
It is not about using "!ordered" or "ordered == false" , it is about : You never changing your ordered to false from script or button events (at least at the part of code you gave us).
For testing, just add this lines to UPDATE(), and you will get result. Press SPACE, and you will get " Debug.Log ("incorrect " + counter);". Because you changed ordered to FALS$$anonymous$$
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
counter = 2;
ordered = false;
TriggerCounter(2);
}
Your answer
Follow this Question
Related Questions
Need hep changing this script to a onclick script.... please help 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
Best way of using animator bools in a script 2 Answers
Multiple Text Objects in a button 0 Answers
Need help with fixing a code 1 Answer