Search Unity

Countdown Timer Bonus Script Almost Working

Discussion in 'Scripting' started by Holocene, Dec 8, 2016.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi,
    I'm trying to activate objects based on a countdown timer.
    I'd like to activate one object if time is > 70sec, another if time < 30sec
    and another if time is <= 0

    Not sure why this won't work beyond: if(timeLeft >= 70),
    doesn't seem to recognize the code after.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CountdownTimerBonusTimer : MonoBehaviour {
    5.  
    6. public float timeLeft = 180.0f;
    7. public GameObject GoldAward3;
    8. public GameObject StarAward3;
    9. public GameObject GoldAward2;
    10. public GameObject StarAward2;
    11. public GameObject GoldAward1;
    12. public GameObject StarAward1;
    13.  
    14. public GameObject projectilePrefab;
    15. private bool canInstantiateProjectile = true;
    16.  
    17.    
    18.      void Update()
    19.      {
    20.          timeLeft -= Time.deltaTime;
    21.  
    22.          {
    23.  
    24.          }
    25.            
    26.              if(timeLeft >= 70)
    27.              {
    28.            
    29.                  if (canInstantiateProjectile)
    30.             {
    31.            
    32.   GoldAward3.SetActive(true);
    33.   StarAward3.SetActive(true);
    34.            
    35.            
    36.                          
    37.                  if(timeLeft < 30)
    38.              {
    39.            
    40.                  if (canInstantiateProjectile)
    41.             {
    42.            
    43.   StarAward2.SetActive(true);
    44.   StarAward2.SetActive(true);
    45.          
    46.            
    47.            
    48.  
    49.                  if(timeLeft == 0)
    50.              {
    51.            
    52.                  if (canInstantiateProjectile)
    53.             {
    54.            
    55.    StarAward1.SetActive(true);
    56.   StarAward1.SetActive(true);
    57.            
    58.             }
    59.  
    60.            
    61.              }}}}}}}
    62.            
    63.            
    64.    
    65.    
    66.      
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I'm going to say your nested if statements are the issue. It is impossible for time to be both >= 70 and < 30.
     
  3. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Yes, that makes sense.
    I tried using else or else if, but that does appear to be the answer.
    Any suggestions on how to separate these statements?