Search Unity

Question How do i force the user to execute The first if statement then he can execute the else if one

Discussion in '2D' started by Stark-12, Sep 9, 2021.

  1. Stark-12

    Stark-12

    Joined:
    Aug 31, 2021
    Posts:
    6
    i have a mini text game here , and i have an if statement but the user can execute what's inside else if before the If one and that breaks the game , how do i prevent that


    my code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8.  
    9. public class MainScript : MonoBehaviour
    10. {
    11.     StateScript State;
    12.  
    13.     [SerializeField] Text PlayerHp;
    14.     [SerializeField] Text StoryText;
    15.     [SerializeField] StateScript FirstState;
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         State = FirstState;
    22.         StoryText.text = State.GetStoryLine();
    23.         PlayerHp.text = State.GetPlayerHp();
    24.  
    25.    
    26.      
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         ManageStates();
    33.     }
    34.  
    35.     void ManageStates()
    36.     {
    37.         StateScript[] NextState = State.GetNextStates();
    38.  
    39.         for (int index = 0; index < NextState.Length; index++)
    40.             if (Input.GetKeyDown(KeyCode.Alpha1 + index))
    41.             {
    42.                 {
    43.                     State = NextState[index];
    44.  
    45.                 }
    46.             }
    47.             else if (Input.GetKeyDown(KeyCode.Return))
    48.             {
    49.  
    50.                 State = NextState[0];
    51.  
    52.             }
    53.  
    54.                 StoryText.text = State.GetStoryLine();
    55.         PlayerHp.text = State.GetPlayerHp();
    56.  
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    your code looks really messy, I think this warrants a full rewrite

    the else if is always available as long as the number is not pressed, which you would need frame perfect timing to pull off
     
  3. Stark-12

    Stark-12

    Joined:
    Aug 31, 2021
    Posts:
    6
    I'm Still learning like still in the basics and stuff ,
    i don't think you understood me

    i want the player to first execute the first if statement
    then it would be available to execute the else if one

    do you know how can i do this ?
     
  4. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    i understand what I want but I was just saying that there are many things wrong with this code, anyway here is a way to do what you want:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class MainScript : MonoBehaviour
    6. {
    7.     StateScript State;
    8.     [SerializeField] Text PlayerHp;
    9.     [SerializeField] Text StoryText;
    10.     [SerializeField] StateScript FirstState;
    11.  
    12. public bool secondIFallowed = false;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         State = FirstState;
    17.         StoryText.text = State.GetStoryLine();
    18.         PlayerHp.text = State.GetPlayerHp();
    19.  
    20.    
    21.     }
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         ManageStates();
    26.     }
    27.     void ManageStates()
    28.     {
    29.         StateScript[] NextState = State.GetNextStates();
    30.         for (int index = 0; index < NextState.Length; index++)
    31.          
    32. if( secondIFallowed == true){
    33.  
    34.   if (Input.GetKeyDown(KeyCode.Return))
    35.             {
    36.  
    37.                 State = NextState[0];
    38. secondIFallowed = false;
    39.  
    40.             }
    41.  
    42. }
    43.  
    44. if (Input.GetKeyDown(KeyCode.Alpha1 + index))
    45.             {
    46.                 {
    47.                     State = NextState[index];
    48. secondIFallowed = true;
    49.                 }
    50.             }
    51.  
    52.  
    53.  
    54.        
    55.                 StoryText.text = State.GetStoryLine();
    56.         PlayerHp.text = State.GetPlayerHp();
    also its really messy that you dont have brackets on your for loop