Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Sep 24, 2013 at 08:12 PM by clunk47 for the following reason:

OP Resolved Issue.

avatar image
0
Question by Rick74 · Sep 24, 2013 at 05:29 PM · javascriptif-statements

Code not entering if statement.

I can't for the life of me figure out why this script won't enter this if statement. I must be overlooking something so I'm hoping a fresh set of eyes can see if there is something I'm overlooking.

Basically the code works fine and get's to the statement "under 1 minute, waiting for "+waiftFor. But when the next set of if statment checks happens ( if (getPlayTime >= waitFor ) nothing happens. It goes on until infinity.

for anyone curious "getPlayTime" is a pause variable replacement for Time.time. And yes, it is keeping track of Time.time as it should. everything is working fine, up until this if statement.

I know this is somewhat hard to follow...it's a work in progress.

 #pragma strict
 
 var playerDamage:                 float = 0;                //records the damage the player has taken
 
 var spawnIsActive:                boolean = true;
 var waveActive:                    boolean = true;            //toggles for controlling the wave
 var spawnEnemies:                 boolean = true;            //
 var bigSpawn:                    boolean = false;
 var firstWaveActive:             boolean = true;
 var secondWaveActive:             boolean = false;
 
 var healthCount:                 int = 10;                //players health count
 var resourceCount:                 int = 100;
 
 var waveText:                     UILabel;                //ui label 
 var resourceText:                 UILabel;
 var waitFor:                    float = 0.0;
 
 var waveLevel:                    int = 0;                //current wave we are on
 var waveLength:                 float = 0;                //how long does the wave last before it stops
 var intermissionTime:             float = 15.0;            //how long to wait inbetween waves
 
 var enemyPrefabs:                 GameObject[];            //array that contains all the enemy types
 var enemyPrefabs02:                GameObject[];                
 var alienSpawnPoints:             Transform[];            //contains the spawn points
 var respawnMinBase:                float = 3.0;            //used to ramp up the amount of spawns
 var respawnMaxBase:             float = 10.0;            //
 private var respawnMin:         float = 3.0;            //
 private var respawnMax:         float = 10.0;            //
 private var waveEndTime:        float = 0.0;
 private var recSpawnActivity:    boolean = false;
 
 var holdSeconds:                  int = 0;
 var holdMinutes:                 int = 0;
 var holdFirstWave:                 boolean;
 var holdSecondWave:                boolean;
 var holdBigWave:                boolean;
 
 var difficulty:                 int = 0;
 var seconds:                     int = 0;
 var minutes:                    int = 0;
 
 private var getClock:            GameObject;                // getting clock and all it's components
 var getPlayTime:                 float = 0.0;
 private var getPlayTimeEnabled: boolean = true;
 
 function Start ()
 {
     UpdateHUD ();
 }
 
 function Update () 
 {    
     getClock = GameObject.FindGameObjectWithTag("clock");
     getPlayTime = getClock.GetComponent(clock).playTime;
     getPlayTimeEnabled = getClock.GetComponent(clock).playTimeEnabled;
 
     if (getPlayTimeEnabled)
     {
         if ( spawnIsActive )
         {
             Clock ();
             if (waveActive)
             {
                 if ( getPlayTime >= waveEndTime )
                 {
                     spawnEnemies = false;
                     FinishWave ();
                     print ( "shutting off wave");
                 }
             }
             if ( minutes >= 1 && firstWaveActive )
             {
                 firstWaveActive = false;
                 secondWaveActive = true;
                 print ( "swapping over to second wave level");
                 waveLength = 5;
             }
             if ( waveLevel == 09 && secondWaveActive )
             {
                 secondWaveActive = false;
                 bigSpawn = true;
                 print ( "swapping over to big spawn");
                 waveLength = 40;
                 FinishWave ();
             }
             if ( minutes >= 4 )
             {
                 difficulty = minutes - 2;
             }
         }
         else 
         {
             recSpawnActivity = true;
         }
     }
 }
 
 function RecordSpawnActivity ()
 {
     holdSeconds = seconds;
     holdMinutes    = minutes;
     holdFirstWave = firstWaveActive;
     holdSecondWave = secondWaveActive;
     holdBigWave = bigSpawn; 
     
     recSpawnActivity = false;    
 }
 
 function FinishWave ()
 {
     waveActive         = false;    
     spawnEnemies     = false;
     var i:             float;    
     i                 = Random.Range (20.0, 30.0);
     waitFor         = getPlayTime + i;
     
     print ("under 1 minute, waiting for "+waitFor);
     
     if ( firstWaveActive && getPlayTime >= waitFor )
     {
         SetNextWave ();                                                    //sets the next wave command
         StartNewWave ();
     }
     if ( secondWaveActive )
     {
         i = Random.Range (12, 18);
         print ("over 1 minute, waiting for "+i);
         yield WaitForSeconds (i);                                        //waits for the the intermission time to pass
         SetNextWave ();                                                    //sets the next wave command
         StartNewWave ();        
     }
     if ( bigSpawn )
     {
         print ("HERE THEY COME!!!");
         yield WaitForSeconds (10);
         SetNextWave ();
         StartNewWave ();
     }
     
 }
 
 function SetNextWave ()                                                //prepares the values for the next wave (how many enemies will be spawn)
 {
     waveLevel ++;                                                       //ups the wave level
 }
 
 function StartNewWave ()
 {
     UpdateHUD ();                                                    //updates the gui
     NewSpawnEnemy ();                                                //spawns new enemy
     waveActive = true;                                                //sets the toggle to spawn enemies
     spawnEnemies = true;
     waveEndTime = getPlayTime + waveLength;
 }
 
 function UpdateHUD ()
 {
     waveText.text = "Wave: " +waveLevel;                            //updates the current waveLevel to the wave GUI
     resourceText.text = "Resources: " +resourceCount;                //updates the current healthLevel to the health GUI
 }
 
 function NewSpawnEnemy ()
 {
     if ( minutes <= 1 && firstWaveActive && !bigSpawn )
     {
         FirstLevelSpawn ();
         print ("first level spawn happening");
     }
     if ( minutes >= 1 && !bigSpawn && secondWaveActive && !firstWaveActive )
     {
         SecondLevelSpawn ();
         print ("second spawn happening");
     }
     if ( bigSpawn && !secondWaveActive )
     {
         BigSpawn ();
         print ("big spawn happening");
     }
 }
 
 function FirstLevelSpawn ()
 {
     var i = difficulty;
     for (; i >= 0; i--)
     {
         var enemyChoice = Random.Range (0, enemyPrefabs.Length);
         var spawnChoice : int;    
         spawnChoice = Random.Range (0, alienSpawnPoints.Length);
         Instantiate (enemyPrefabs[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         print ( "1 enemy spawned");
     }
 }    
 
 function SecondLevelSpawn ()
 {    
     var i = difficulty;
     for (; i >= 0; i--)
     {
         var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
         var spawnChoice : int;    
         if ( enemyChoice == 1 )
         {
             var a = 1;
             for (; a >= 0; a--)
             {
                 spawnChoice = Random.Range (0, alienSpawnPoints.Length);
                 Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
             }
         }
         if ( enemyChoice == 0 )
         {
             spawnChoice = Random.Range (0, alienSpawnPoints.Length);
             Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         }
     }
     print ( "second level spawn finished" );
 }
 
 function BigSpawn ()
 {
     print ("Big Spawn!");
     var i = 3;
     for (; i >= 0; i--)
     {
         var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
         var spawnChoice : int;    
         if ( enemyChoice == 1 )
         {
             var a = 1;
             for (; a >= 0; a--)
             {
                 spawnChoice = Random.Range (0, alienSpawnPoints.Length);
                 Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
             }
         }
         if ( enemyChoice == 0 )
         {
             spawnChoice = Random.Range (0, alienSpawnPoints.Length);
             Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         }
         print ( "big spawn loop "+i);
     }
     bigSpawn = false;
     secondWaveActive = true;
     waveLength = 5;
     print ("big spawn finished");
 }
 
 function Clock ()
 {
     seconds = getPlayTime % 60;
     minutes = getPlayTime / 60;
 }
Comment
Add comment · Show 3
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 clunk47 · Sep 24, 2013 at 05:53 PM 0
Share

The only place you have getPlayTime >= waitFor is in

  if (firstWaveActive && getPlayTime >= waitFor)

Only if the FIRST wave is active. Should this be for all waves? Try Debug.Log or Print to send a message to the console for this condition, tell me what happens.

 if (firstWaveActive && getPlayTime >= waitFor)
 {
     Debug.Log("Code is firing.");
 }
avatar image Rick74 · Sep 24, 2013 at 06:06 PM 0
Share

Hey clunk, yeah I've placed print commands inside that if statement, and I check on the hierarchy to make sure that all parameters are met, yet nothing happens. So placing in your Debug code, nothing happens. After getPlayTime catches up to and surpasses waitFor...no print log, no debug...nothin. It's just not going in there.

And yes, firstWaveActive is true at this point.

The code stops just before that if statement. So it will print out "under 1 $$anonymous$$ute, waiting for " +waitFor. So I know I'm getting that far at least.

avatar image Seth-Bergman · Sep 24, 2013 at 08:14 PM 0
Share

please post your answer as an answer and accept it..

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Rick74 · Sep 24, 2013 at 08:09 PM

OK nevermind, I solved this. The reason that it never entered the if statement was because at the moment the code goes into the function, the statement isn't true. Because the function isn't updating every frame, it will never check to see when that statement will become true. Thus, we get locked in the function.

Hope that makes sense to anyone following my silly little drama...lol. I'm a noob :(

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

EXTREMELY basic scripting question. 2 Answers

really strange thing with "if" statment not working well 1 Answer

Object not moving on KeyPress? 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