Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by greens356 · Apr 18, 2016 at 01:28 PM · timedebuggingif statementlogic

Condition not occuring in script

My game is running well but I am having trouble with a certain condition not firing and I believe it is because the part of the script I need is not reachable, but I do not know the reason why.

It seems that once I call setRoundOver() and make the gameTime == roundIntervalTime, the gameTime does equal the roundIntervalTime, yet I can not reach the if(gameTime==roundIntervalTime) on the Round.cs file. I am not sure why it is not making it there.

The way that my game is set up is all based on the time (gameTime and roundIntervalTime and elapsedTime) which then determines 1. What round you are in and 2. Whether you are in a break or not (which occurs between rounds).

The following is the two scripts that are interacting with each other. Any help is appreciated, thanks!

Round.cs file

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Round : MonoBehaviour {
 
     public bool onBreakTime; //flag of whether starting a new game or not
     bool isRoundOver;
     bool gameOver;
 
     public int round;
     public int timeBetweenRounds;
     public int gameTime; //the amount of time passed represented as an int
     public int roundIntervalTime;
     public int sliderValue;
 
     public float startTime;
     public float elapsedTime;
     public double difficultyMultiplier;
     float effectsDisplayTime;
     public Cat cat;
 
     Text roundText;
     public Slider timerSlider;
 
     void Awake (){
         roundText = GetComponent<Text>();
     }
 
     void Start(){
         onBreakTime = true;
         round = 0;
         gameTime = 0;
         elapsedTime = 0;
         sliderValue = 0;
         timeBetweenRounds = 5;
         roundIntervalTime = 20;
         difficultyMultiplier = 0.05;
         startTime = Time.time;
         isRoundOver = true;
         gameOver = false;
     }
     
     public int calculateDifficulty (int roundTime){
         /* shave off (difficultyMultiplier*100)% of the round time each time to increase difficulty*/
 
         double temp = 0;
         int temp2 = 0;
         double roundTime2 = roundTime;
         temp = roundTime2 * difficultyMultiplier; //5% off each round
         temp = roundTime2 - temp;
         temp2 = (int)System.Math.Floor(temp);
         return temp2;
     }
 
     public float calculateTimeLeft(int roundTime, float elapsedTime){
         float timeLeft = 0;
         timeLeft = elapsedTime / roundTime;
         return timeLeft;
     }
 
     public void resetTimers(){
         gameTime = 0;
         elapsedTime = 0;
         startTime = Time.time;
     }
 
     public void setGameOver(){
         gameOver = true;
     }
 
     public bool getGameOverStatus(){
         return gameOver;
     }
 
     public void setRoundOver(){
         gameTime = roundIntervalTime;
         Debug.Log("The gameTime has been set to: " + gameTime + " and the round time is currently: " + roundIntervalTime);
     }
 
     // Update is called once per frame
     void Update (){
         cat.checkIfPlayerLost();
 
         if (!gameOver)
         {
             elapsedTime = Time.time - startTime; //Get the actual float value of elapsed time from Unity system
             //Debug.Log(elapsedTime); //print to console for debugging
             gameTime = (int)System.Math.Floor(elapsedTime); //truncate the time to an integer value, rounding down
             //Debug.Log(gameTime);
 
             if (onBreakTime)
             {
                 if (gameTime == timeBetweenRounds)
                 {
                     round++;
                     resetTimers();
                     onBreakTime = false;
                 }
             }
             else {
                 Debug.Log("Entering the Else part 1");
                 timerSlider.value = calculateTimeLeft(roundIntervalTime, elapsedTime);
 
                 //if the cat is not spawned and the round is over, spawn the cat
                 if (!(cat.isCatSpawned()) && isRoundOver)
                 {
                     cat.Spawn();
                 }
 
                 isRoundOver = false;
 
                 //if it is the end of the round and you didnt defeat the cat
                 if (gameTime == roundIntervalTime)
                 {
                     Debug.Log("==WE ARE ENTERING THE ELSE PART 2==");
                     Debug.Log("The current gameTime time is: " + gameTime);
                     //roundIntervalTime = calculateDifficulty(roundIntervalTime);
                     //Debug.Log("The new game round time is: " + roundIntervalTime);
                     resetTimers();
                     timerSlider.value = 0;
                     onBreakTime = true;
                     //at end of round, is the cat spawn? if so, remove that spawn point from the list
                     if (cat.isCatSpawned())
                     {
                         cat.removeSpawnPoint();
                     }
                     if (cat.catCurrentHealth <= 0)
                     {
                         // move Despawn here?
                         cat.Despawn();
                     }
                     //cat.Despawn(); put it back if need be
                     isRoundOver = true;
                 }
             }
             roundText.text = "Round: " + round; //display the round
         }
     }
 
     void FixedUpdate (){
     }
 }

Cat.cs file

 using UnityEngine;
 using System.Collections;
 
 public class Cat : MonoBehaviour {
 
     //bool isCatSpawned;
     int i;
     int c;
     int counter;
     int catStartingHealth;
     int roomsDestroyedToWin;
     public float catCurrentHealth;
 
     public GameObject cat;
     public Transform[] spawnPoints;
     Vector3 nullLocation;
     //public ArrayList possibleSpawns;
 
     public Round round;
 
     void Awake (){
     }
 
     // Use this for initialization
     void Start (){
         //i = 0;
         c = 0;
         catStartingHealth = 100;
         catCurrentHealth = catStartingHealth;
         nullLocation.Set(999, 999, 999);
         counter = 0;
         roomsDestroyedToWin = 7;
     }
     
     // Update is called once per frame
     void Update (){
         if (catCurrentHealth <= 0){
             Debug.Log("You defeated the cat in the " + spawnPoints[i]);
             round.setRoundOver();
             Despawn();
         }
     }
 
     public bool isCatSpawned(){
         if (cat.activeSelf){
             return true;
         }
         else{
             return false;
         }
     }
 
     public void Spawn(){
         //Choose a random index and store in i, then set the cats position, and then spawn (unhide)
         resetCatHealth();
         //Random.seed = ;
         i = Random.Range(0, spawnPoints.Length);
         Debug.Log("first random pick in Spawn(): " + i);
 
         while (spawnPoints[i].position.Equals(nullLocation)){
             i = Random.Range(0, spawnPoints.Length);
             Debug.Log("second random pick in Spawn() is (repick): " + i);
         }
         cat.transform.position = spawnPoints[i].transform.position;
         cat.SetActive(true);
         Debug.Log("Spawning cat at: " + spawnPoints[i]);
         Debug.Log("End of Spawn().  i = " + i);
     }
 
     public void Despawn(){
         //hide the cat, turn spawned flag off, end the round (????)
         cat.SetActive(false);
     }
 
     public void takeDamage(float amt){
         catCurrentHealth -= amt;
     }
 
     public void resetCatHealth(){
         catCurrentHealth = catStartingHealth;
     }
 
     public void removeSpawnPoint(){
         Debug.Log("Setting " + spawnPoints[i] + " to (999,999,999)");
         spawnPoints[i].position = nullLocation;
         Debug.Log(spawnPoints[i] + "is set to: " + spawnPoints[i].position);
     }
 
     public void checkIfPlayerLost(){
         //Debug.Log("Entering checkIfPlayerLost(), and checking...");
         counter = 0;
         for (c = 0; c < spawnPoints.Length; c++){
             if (spawnPoints[c].position.Equals(nullLocation)){
                 counter++;
                 if (counter == roomsDestroyedToWin){
                     //Debug.Log("The game is over! You lost mofo");
                     round.setGameOver();
                 }
             }
         }
         //Debug.Log("The game is not over, proceed and try to stop that kitty!");
     }
 }
 

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Time.realTimeSinceStartup and Time.unscaledTime in Breakpoint 0 Answers

Provide user virtual currency after every 24 hrs. 1 Answer

How to activate/deactivate object in if statements? 2 Answers

Screensaver when device is idle 1 Answer

Unity how to find load time? 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