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 /
avatar image
0
Question by JanaUnityCiric · Jun 11, 2018 at 07:41 AM · scripting problem

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 ();
     }
 }
     

}

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
0

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

Comment
Add comment · Show 3 · 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 JanaUnityCiric · Jun 11, 2018 at 08:20 AM 0
Share

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.

avatar image bakir-omarov · Jun 11, 2018 at 08:24 AM 0
Share

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

avatar image bakir-omarov bakir-omarov · Jun 11, 2018 at 08:25 AM 0
Share

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

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

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

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


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