Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to fix the text

Discussion in 'Scripting' started by 11Buff, Sep 28, 2016.

  1. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    I FIXED IT! THANKS THOUGH!


    I am checking something every frame to see if it's true. If it's true it will change the text. But there is a bool that sets the text to turn off. That is called every frame too making the text change. Please tell me how to fix. Here is my code.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Calls : MonoBehaviour {
    7.  
    8.     public bool onCall = false;
    9.     public bool responded = false;
    10.     public float timeBetweenCall;
    11.     private float callDifficulty;
    12.     public int busyOfficers = 0;
    13.     public float Officers;
    14.     public float officerReturning;
    15.     public float callsAnswered;
    16.     public float timeToAnswer;
    17.  
    18.     //CALL VARIABLES
    19.     private bool OnCall1 = false;
    20.     private bool OnCall2 = false;
    21.     private bool OnCall3 = false;
    22.  
    23.  
    24.     [SerializeField]
    25.     public Text callName = null;
    26.  
    27.     [SerializeField]
    28.     public Text callsAnsweredText = null;
    29.  
    30.     [SerializeField]
    31.     public Text officers = null;
    32.  
    33.     [SerializeField]
    34.     public Text callDesc = null;
    35.  
    36.     [SerializeField]
    37.     public Text busyOfficersText = null;
    38.  
    39.     [SerializeField]
    40.     public InputField officersResponding;
    41.  
    42.  
    43.     // Use this for initialization
    44.     void Start() {
    45.         OnCall1 = true;
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void Update() {
    50.  
    51.         if(busyOfficers <= 0)
    52.         {
    53.             busyOfficers = 0;
    54.         }
    55.  
    56.         callsAnsweredText.text = "Calls Answered: " + callsAnswered;
    57.  
    58.         officers.text = "Officers: " + Officers;
    59.  
    60.         busyOfficersText.text = "Busy Officers: " + busyOfficers;
    61.  
    62.         officers.text = "Officers: " + Officers;
    63.  
    64.  
    65.         //Start of calling the calls
    66.  
    67.         if (OnCall1 == true)
    68.         {
    69.             StartCoroutine("Call1");
    70.             OnCall1 = false;
    71.         }
    72.  
    73.         if (OnCall2 == true)
    74.         {
    75.             StartCoroutine("Call2");
    76.             OnCall2 = false;
    77.         }
    78.  
    79.         if(OnCall3 == true)
    80.         {
    81.             StartCoroutine("Call3");
    82.             OnCall3 = false;
    83.         }
    84.  
    85.         //End of calling the calls
    86.  
    87.     }
    88.  
    89.     //CALLS
    90.  
    91.     IEnumerator Call1()
    92.     {
    93.  
    94.         callName.text = "No call!";
    95.         callDesc.text = "Please wait for a call!";
    96.  
    97.         yield return new WaitForSeconds(6);
    98.  
    99.         responded = false;
    100.         onCall = true;
    101.         callDifficulty = 1;
    102.         callName.text = "Disturbance - Man";
    103.         callDesc.text = "A man has reported that their neighbour has been partying and disturbing the peace of the neighborhood.";
    104.         OnCall2 = true;
    105.     }
    106.  
    107.     IEnumerator Call2()
    108.     {
    109.  
    110.         callName.text = "No call!";
    111.         callDesc.text = "Please wait for a call!";
    112.  
    113.         yield return new WaitForSeconds(timeToAnswer);
    114.  
    115.         responded = false;
    116.         onCall = true;
    117.         callDifficulty = 1;
    118.         callName.text = "Disturbance - Woman";
    119.         callDesc.text = "A woman has reported that their neighbour has been partying and disturbing the peace of the neighborhood.";
    120.         OnCall3 = true;
    121.     }
    122.  
    123.     IEnumerator Call3()
    124.     {
    125.  
    126.         callName.text = "No call!";
    127.         callDesc.text = "Please wait for a call!";
    128.  
    129.         yield return new WaitForSeconds(timeToAnswer);
    130.  
    131.         responded = false;
    132.         onCall = true;
    133.         callDifficulty = 1;
    134.         callName.text = "Call3";
    135.         callDesc.text = "This is call 3";
    136.         //OnCall1 = true;
    137.  
    138.     }
    139.  
    140.     //END OF CALLS
    141.  
    142.     //Adding officers / Removing
    143.  
    144.     public void Responding()
    145.     {
    146.  
    147.         if(Officers >= 1)
    148.         {
    149.          
    150.         }
    151.         else if(Officers <= 0)
    152.         {
    153.             return;
    154.     }
    155.  
    156.         if(onCall == false)
    157.         {
    158.          
    159.         }
    160.         else if (onCall == true)
    161.         {
    162.             //Getting officers Responding
    163.             if (officersResponding.text == "1")
    164.             {
    165.                 if (Officers >= 1)
    166.                 {
    167.                     onCall = false;
    168.                     StartCoroutine("OneSent");
    169.                     ResetOfficersResponding();
    170.                 }
    171.             }
    172.  
    173.             if (officersResponding.text == "2")
    174.             {
    175.                 if (Officers >= 2)
    176.                 {
    177.                     onCall = false;
    178.                     StartCoroutine("TwoSent");
    179.                     ResetOfficersResponding();
    180.                 }
    181.             }
    182.  
    183.             if (officersResponding.text == "3")
    184.             {
    185.                 if (Officers >= 3)
    186.                 {
    187.                     onCall = false;
    188.                     StartCoroutine("ThreeSent");
    189.                     ResetOfficersResponding();
    190.                 }
    191.             }
    192.  
    193.             if (officersResponding.text == "4")
    194.             {
    195.                 if (Officers >= 4)
    196.                 {
    197.                     onCall = false;
    198.                     StartCoroutine("FourSent");
    199.                     ResetOfficersResponding();
    200.                 }
    201.             }
    202.  
    203.             if (officersResponding.text == "5")
    204.             {
    205.                 if (Officers >= 5)
    206.                 {
    207.                     StartCoroutine("FiveSent");
    208.                     ResetOfficersResponding();
    209.                 }
    210.             }
    211.  
    212.             if (officersResponding.text == "6")
    213.             {
    214.                 if (Officers >= 6)
    215.                 {
    216.                     onCall = false;
    217.                     StartCoroutine("SixSent");
    218.                     ResetOfficersResponding();
    219.                 }
    220.             }
    221.  
    222.             if (officersResponding.text == "7")
    223.             {
    224.                 if (Officers >= 7)
    225.                 {
    226.                     onCall = false;
    227.                     StartCoroutine("SevenSent");
    228.                     ResetOfficersResponding();
    229.                 }
    230.             }
    231.  
    232.             if (officersResponding.text == "8")
    233.             {
    234.                 if (Officers >= 8)
    235.                 {
    236.                     onCall = false;
    237.                     StartCoroutine("EightSent");
    238.                     ResetOfficersResponding();
    239.                 }
    240.             }
    241.  
    242.             if (officersResponding.text == "9")
    243.             {
    244.                 if (Officers >= 9)
    245.                 {
    246.                     onCall = false;
    247.                     StartCoroutine("NineSent");
    248.                     ResetOfficersResponding();
    249.                 }
    250.             }
    251.  
    252.             if (officersResponding.text == "10")
    253.             {
    254.                 if (Officers >= 10)
    255.                 {
    256.                     onCall = false;
    257.                     StartCoroutine("TenSent");
    258.                     ResetOfficersResponding();
    259.                 }
    260.             }
    261.         }
    262.     }
    263.  
    264.  
    265.  
    266. IEnumerator OneSent()
    267.     {
    268.         onCall = false;
    269.         busyOfficers += 1;
    270.         Officers -= 1;
    271.         responded = true;
    272.         yield return new WaitForSeconds(officerReturning);
    273.         Officers += 1;
    274.         busyOfficers -= 1;
    275.         callsAnswered += 1;
    276.         StartCoroutine("ReturnOfficers");
    277.     }
    278.  
    279.     IEnumerator TwoSent()
    280.     {
    281.         onCall = false;
    282.         busyOfficers += 2;
    283.         Officers -= 2;
    284.         responded = true;
    285.         yield return new WaitForSeconds(officerReturning);
    286.         Officers += 2;
    287.         busyOfficers -= 2;
    288.         callsAnswered += 1;
    289.         StartCoroutine("ReturnOfficers");
    290.     }
    291.  
    292.     IEnumerator ThreeSent()
    293.     {
    294.         onCall = false;
    295.         busyOfficers += 3;
    296.         Officers -= 3;
    297.         responded = true;
    298.         yield return new WaitForSeconds(officerReturning);
    299.         Officers += 3;
    300.         busyOfficers -= 3;
    301.         callsAnswered += 1;
    302.         StartCoroutine("ReturnOfficers");
    303.     }
    304.  
    305.     IEnumerator FourSent()
    306.     {
    307.         onCall = false;
    308.         busyOfficers += 4;
    309.         Officers -= 4;
    310.         responded = true;
    311.         yield return new WaitForSeconds(officerReturning);
    312.         Officers += 4;
    313.         busyOfficers -= 4;
    314.         callsAnswered += 1;
    315.         StartCoroutine("ReturnOfficers");
    316.     }
    317.  
    318.     IEnumerator FiveSent()
    319.     {
    320.         onCall = false;
    321.         busyOfficers += 5;
    322.         Officers -= 5;
    323.         responded = true;
    324.         yield return new WaitForSeconds(officerReturning);
    325.         Officers += 5;
    326.         busyOfficers -= 5;
    327.         callsAnswered += 1;
    328.         StartCoroutine("ReturnOfficers");
    329.     }
    330.  
    331.     IEnumerator SixSent()
    332.     {
    333.         onCall = false;
    334.         busyOfficers += 6;
    335.         Officers -= 6;
    336.         responded = true;
    337.         yield return new WaitForSeconds(officerReturning);
    338.         Officers += 6;
    339.         busyOfficers -= 6;
    340.         callsAnswered += 1;
    341.         StartCoroutine("ReturnOfficers");
    342.     }
    343.  
    344.     IEnumerator SevenSent()
    345.     {
    346.         onCall = false;
    347.         busyOfficers += 7;
    348.         Officers -= 7;
    349.         responded = true;
    350.         yield return new WaitForSeconds(officerReturning);
    351.         Officers += 7;
    352.         busyOfficers -= 7;
    353.         callsAnswered += 1;
    354.         StartCoroutine("ReturnOfficers");
    355.     }
    356.  
    357.     IEnumerator EightSent()
    358.     {
    359.         onCall = false;
    360.         busyOfficers += 8;
    361.         Officers -= 8;
    362.         responded = true;
    363.         yield return new WaitForSeconds(officerReturning);
    364.         Officers +=8;
    365.         busyOfficers -= 8;
    366.         callsAnswered += 1;
    367.         StartCoroutine("ReturnOfficers");
    368.     }
    369.  
    370.     IEnumerator NineSent()
    371.     {
    372.         onCall = false;
    373.         busyOfficers += 9;
    374.         Officers -= 9;
    375.         responded = true;
    376.         yield return new WaitForSeconds(officerReturning);
    377.         Officers += 9;
    378.         busyOfficers -= 9;
    379.         callsAnswered += 1;
    380.         StartCoroutine("ReturnOfficers");
    381.     }
    382.  
    383.     IEnumerator TenSent()
    384.     {
    385.         onCall = false;
    386.         busyOfficers += 10;
    387.         Officers -= 10;
    388.         responded = true;
    389.         yield return new WaitForSeconds(officerReturning);
    390.         Officers += 10;
    391.         busyOfficers -= 10;
    392.         callsAnswered += 1;
    393.         StartCoroutine("ReturnOfficers");
    394.     }
    395.  
    396.  
    397.     IEnumerator ReturnOfficers()
    398.     {
    399.         yield return new WaitForSeconds(5);
    400.         busyOfficers -= 1;
    401.         Officers += 1;
    402.         OnCall1 = false;
    403.     }
    404.  
    405.     void ResetOfficersResponding()
    406.     {
    407.         officersResponding.text = "Officers Responding";
    408.     }
    409.  
    410. }
    411.  
    412.  
    Thanks,
     
    Last edited: Sep 28, 2016
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Just curious, but why are you having to check if the value is true in update instead of just starting the coroutine when the value is true?

    Honestly, looking at your code, it could be written a lot better considering most of what I'm seeing is repeating code. This would not only help you, but also help others with debugging your code. You have a lot of stuff that is pretty much repeating code with simple value changes. Your onesent, twosent, etc coroutines could easily be passed a value for example.
     
  3. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    I know. I don't really care that my code is messy, but I am going to fix it and make is smaller when I have the mechanics finished.
     
  4. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Not to bash, but you're building yourself a spaghetti code. With every line of code that you add that depends on this, it will be harder to fix it.
    You're already experiencing interdependencies that cause bugs - this will just grow. It's not a matter of making the codebase smaller, but (re)usable.
    Although it may sound arrogant from my side, do yourself a favor and don't add new functionalities until you fix this. In the end it might at the very least save you from responses like "throw this away and rewrite it" when you ask a question in the future.

    Also - people hesitate to invest time into investigating bugs with code that's clearly a mess, so you'll wait longer for answers too. This topic is a good example of that - who's going to check interdependencies in a 400 line class on a whim?
     
  5. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22

    The problem with my code was that I just needed to add a WaitForSeconds. I can read this code. Thanks though. All I need to do now is add the "calls"