Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The text is bugging out!

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

  1. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    I have a problem with my code, where I call a function, and it loads properly, but when the text changes, it keeps going funny (changing from what I want it to be, to what is in the next function / at the top of the function. And I am not sure how to fix it. 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.         }
    71.  
    72.         if (OnCall2 == true)
    73.         {
    74.             StartCoroutine("Call2");
    75.         }
    76.  
    77.         if(OnCall3 == true)
    78.         {
    79.             StartCoroutine("Call3");
    80.         }
    81.  
    82.         //End of calling the calls
    83.  
    84.  
    85.         if(OnCall1 == true)
    86.         {
    87.             StartCoroutine("Call1");
    88.         }
    89.         else if(OnCall2 == true)
    90.         {
    91.             StartCoroutine("Call2");
    92.         }
    93.  
    94.     }
    95.  
    96.     //CALLS
    97.  
    98.     IEnumerator Call1()
    99.     {
    100.  
    101.         callName.text = "No call!";
    102.         callDesc.text = "Please wait for a call!";
    103.  
    104.         yield return new WaitForSeconds(6);
    105.  
    106.         responded = false;
    107.         onCall = true;
    108.         callDifficulty = 1;
    109.         callName.text = "Disturbance - Man";
    110.         callDesc.text = "A man has reported that their neighbour has been partying and disturbing the peace of the neighborhood.";
    111.         OnCall2 = true;
    112.         OnCall1 = false;
    113.     }
    114.  
    115.     IEnumerator Call2()
    116.     {
    117.  
    118.         callName.text = "No call!";
    119.         callDesc.text = "Please wait for a call!";
    120.  
    121.         yield return new WaitForSeconds(timeToAnswer);
    122.  
    123.         responded = false;
    124.         onCall = true;
    125.         callDifficulty = 1;
    126.         callName.text = "Disturbance - Woman";
    127.         callDesc.text = "A woman has reported that their neighbour has been partying and disturbing the peace of the neighborhood.";
    128.         OnCall3 = true;
    129.         OnCall2 = false;
    130.     }
    131.  
    132.     IEnumerator Call3()
    133.     {
    134.  
    135.         callName.text = "No call!";
    136.         callDesc.text = "Please wait for a call!";
    137.  
    138.         yield return new WaitForSeconds(timeToAnswer);
    139.  
    140.         responded = false;
    141.         onCall = true;
    142.         callDifficulty = 1;
    143.         callName.text = "Call3";
    144.         callDesc.text = "This is call 3";
    145.         //OnCall1 = true;
    146.         OnCall3 = false;
    147.     }
    148.  
    149.     //END OF CALLS
    150.  
    151.     //Adding officers / Removing
    152.  
    153.     public void Responding()
    154.     {
    155.  
    156.         if(Officers >= 1)
    157.         {
    158.      
    159.         }
    160.         else if(Officers <= 0)
    161.         {
    162.             return;
    163.     }
    164.  
    165.         if(onCall == false)
    166.         {
    167.      
    168.         }
    169.         else if (onCall == true)
    170.         {
    171.             //Getting officers Responding
    172.             if (officersResponding.text == "1")
    173.             {
    174.                 if (Officers >= 1)
    175.                 {
    176.                     onCall = false;
    177.                     StartCoroutine("OneSent");
    178.                     ResetOfficersResponding();
    179.                 }
    180.             }
    181.  
    182.             if (officersResponding.text == "2")
    183.             {
    184.                 if (Officers >= 2)
    185.                 {
    186.                     onCall = false;
    187.                     StartCoroutine("TwoSent");
    188.                     ResetOfficersResponding();
    189.                 }
    190.             }
    191.  
    192.             if (officersResponding.text == "3")
    193.             {
    194.                 if (Officers >= 3)
    195.                 {
    196.                     onCall = false;
    197.                     StartCoroutine("ThreeSent");
    198.                     ResetOfficersResponding();
    199.                 }
    200.             }
    201.  
    202.             if (officersResponding.text == "4")
    203.             {
    204.                 if (Officers >= 4)
    205.                 {
    206.                     onCall = false;
    207.                     StartCoroutine("FourSent");
    208.                     ResetOfficersResponding();
    209.                 }
    210.             }
    211.  
    212.             if (officersResponding.text == "5")
    213.             {
    214.                 if (Officers >= 5)
    215.                 {
    216.                     StartCoroutine("FiveSent");
    217.                     ResetOfficersResponding();
    218.                 }
    219.             }
    220.  
    221.             if (officersResponding.text == "6")
    222.             {
    223.                 if (Officers >= 6)
    224.                 {
    225.                     onCall = false;
    226.                     StartCoroutine("SixSent");
    227.                     ResetOfficersResponding();
    228.                 }
    229.             }
    230.  
    231.             if (officersResponding.text == "7")
    232.             {
    233.                 if (Officers >= 7)
    234.                 {
    235.                     onCall = false;
    236.                     StartCoroutine("SevenSent");
    237.                     ResetOfficersResponding();
    238.                 }
    239.             }
    240.  
    241.             if (officersResponding.text == "8")
    242.             {
    243.                 if (Officers >= 8)
    244.                 {
    245.                     onCall = false;
    246.                     StartCoroutine("EightSent");
    247.                     ResetOfficersResponding();
    248.                 }
    249.             }
    250.  
    251.             if (officersResponding.text == "9")
    252.             {
    253.                 if (Officers >= 9)
    254.                 {
    255.                     onCall = false;
    256.                     StartCoroutine("NineSent");
    257.                     ResetOfficersResponding();
    258.                 }
    259.             }
    260.  
    261.             if (officersResponding.text == "10")
    262.             {
    263.                 if (Officers >= 10)
    264.                 {
    265.                     onCall = false;
    266.                     StartCoroutine("TenSent");
    267.                     ResetOfficersResponding();
    268.                 }
    269.             }
    270.         }
    271.     }
    272.  
    273.  
    274.  
    275. IEnumerator OneSent()
    276.     {
    277.         onCall = false;
    278.         busyOfficers += 1;
    279.         Officers -= 1;
    280.         responded = true;
    281.         yield return new WaitForSeconds(officerReturning);
    282.         Officers += 1;
    283.         busyOfficers -= 1;
    284.         callsAnswered += 1;
    285.         StartCoroutine("ReturnOfficers");
    286.     }
    287.  
    288.     IEnumerator TwoSent()
    289.     {
    290.         onCall = false;
    291.         busyOfficers += 2;
    292.         Officers -= 2;
    293.         responded = true;
    294.         yield return new WaitForSeconds(officerReturning);
    295.         Officers += 2;
    296.         busyOfficers -= 2;
    297.         callsAnswered += 1;
    298.         StartCoroutine("ReturnOfficers");
    299.     }
    300.  
    301.     IEnumerator ThreeSent()
    302.     {
    303.         onCall = false;
    304.         busyOfficers += 3;
    305.         Officers -= 3;
    306.         responded = true;
    307.         yield return new WaitForSeconds(officerReturning);
    308.         Officers += 3;
    309.         busyOfficers -= 3;
    310.         callsAnswered += 1;
    311.         StartCoroutine("ReturnOfficers");
    312.     }
    313.  
    314.     IEnumerator FourSent()
    315.     {
    316.         onCall = false;
    317.         busyOfficers += 4;
    318.         Officers -= 4;
    319.         responded = true;
    320.         yield return new WaitForSeconds(officerReturning);
    321.         Officers += 4;
    322.         busyOfficers -= 4;
    323.         callsAnswered += 1;
    324.         StartCoroutine("ReturnOfficers");
    325.     }
    326.  
    327.     IEnumerator FiveSent()
    328.     {
    329.         onCall = false;
    330.         busyOfficers += 5;
    331.         Officers -= 5;
    332.         responded = true;
    333.         yield return new WaitForSeconds(officerReturning);
    334.         Officers += 5;
    335.         busyOfficers -= 5;
    336.         callsAnswered += 1;
    337.         StartCoroutine("ReturnOfficers");
    338.     }
    339.  
    340.     IEnumerator SixSent()
    341.     {
    342.         onCall = false;
    343.         busyOfficers += 6;
    344.         Officers -= 6;
    345.         responded = true;
    346.         yield return new WaitForSeconds(officerReturning);
    347.         Officers += 6;
    348.         busyOfficers -= 6;
    349.         callsAnswered += 1;
    350.         StartCoroutine("ReturnOfficers");
    351.     }
    352.  
    353.     IEnumerator SevenSent()
    354.     {
    355.         onCall = false;
    356.         busyOfficers += 7;
    357.         Officers -= 7;
    358.         responded = true;
    359.         yield return new WaitForSeconds(officerReturning);
    360.         Officers += 7;
    361.         busyOfficers -= 7;
    362.         callsAnswered += 1;
    363.         StartCoroutine("ReturnOfficers");
    364.     }
    365.  
    366.     IEnumerator EightSent()
    367.     {
    368.         onCall = false;
    369.         busyOfficers += 8;
    370.         Officers -= 8;
    371.         responded = true;
    372.         yield return new WaitForSeconds(officerReturning);
    373.         Officers +=8;
    374.         busyOfficers -= 8;
    375.         callsAnswered += 1;
    376.         StartCoroutine("ReturnOfficers");
    377.     }
    378.  
    379.     IEnumerator NineSent()
    380.     {
    381.         onCall = false;
    382.         busyOfficers += 9;
    383.         Officers -= 9;
    384.         responded = true;
    385.         yield return new WaitForSeconds(officerReturning);
    386.         Officers += 9;
    387.         busyOfficers -= 9;
    388.         callsAnswered += 1;
    389.         StartCoroutine("ReturnOfficers");
    390.     }
    391.  
    392.     IEnumerator TenSent()
    393.     {
    394.         onCall = false;
    395.         busyOfficers += 10;
    396.         Officers -= 10;
    397.         responded = true;
    398.         yield return new WaitForSeconds(officerReturning);
    399.         Officers += 10;
    400.         busyOfficers -= 10;
    401.         callsAnswered += 1;
    402.         StartCoroutine("ReturnOfficers");
    403.     }
    404.  
    405.  
    406.     IEnumerator ReturnOfficers()
    407.     {
    408.         yield return new WaitForSeconds(5);
    409.         busyOfficers -= 1;
    410.         Officers += 1;
    411.         OnCall1 = false;
    412.     }
    413.  
    414.     void ResetOfficersResponding()
    415.     {
    416.         officersResponding.text = "Officers Responding";
    417.     }
    418.  
    419. }
    420.  
    Thanks for the help!
     
    Last edited: Sep 28, 2016
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    Please use CODE tags
     
  3. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    How?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
  5. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    You keep starting coroutines every frame.

    Code (CSharp):
    1.         if (OnCall1 == true)
    2.         {
    3.             StartCoroutine("Call1");
    4.         }
    This keeps launching Call1 every frame until you make ti false (like 6 seconds later). So a mess ensues.

    Try making OnCall1 = false within that if.
     
    11Buff likes this.
  7. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    O, ok!
     
  8. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    Wait,
    Wait, it sets it to false every frame. Which makes the text never change.
     
  9. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    What are you talking about?
     
  10. 11Buff

    11Buff

    Joined:
    Feb 26, 2016
    Posts:
    22
    What you said to change, is in the Update function, so the "Call" only shows for a split second, then it goes back to no call.
     
  11. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    But the coroutine has already started so you don't need it to be called again.

    I think you need to do some reading on how coroutines work.

    Also, your code over relies on coroutines, you shouldn't use them for every little thing.