- Home /
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!");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612075043im_/https://answers.unity.com/themes/thub/images/avi.jpg)