Cannot create game over parameter
I'm trying to create a condition in which the game throws a game over. The accessor/mutator I'm using is:
public void SetGameOverState()
{
gameOverState += gameObj.gameOverVal;
mValCheck = gameOverState;
}
public int GetGameOverState()
{
return gameObj.gameOverVal;
}
And it's running in this code:
void PlaceNewObject()
{
gameOverState = gameOver.GetGameOverState();
//Do stuff
if (gameOverState < 3)
{
//Do stuff
}
else
{
SceneManager.LoadScene("GameOver");
}
}
In order to get values for the state, on a script attached to the game object itself, I run the following statement with a public int value:
else if ((transform.position.y <= -4F) && transform.position.x == 0.65F)
{
gameOverVal++;
}
My issue is that I know that the elseif statement works, because I tested it by replacing gameOverVal++ with Destroy(gameObject) and, sure enough, the object was destroyed when those two conditions were met. But it seems like it doesn't care for the increment I specified. What do?
Answer by Pengawk · Feb 22, 2016 at 11:33 PM
For this to work, I've figured it out through some trial and error. To get anywhere, the class and its functions for the "GameOverState" must be static.
public static void SetGameOverState(int val)
{
gameOverState += val;
}
public static int GetGameOverState()
{
return gameOverState;
}
Answer by jgodfrey · Feb 21, 2016 at 03:28 PM
I'm not sure that I follow all of the above, but this looks problematic...
transform.position.x == 0.65F
It's never a good idea to do an equality check on a REAL value. Due to floating point inaccuracies, this will fail in all kinds of unpredictable ways. Instead, you should see if the position.x value is close enough to 0.65F. You'll have to decide what is "close enough".
For instance, you could do something like this:
... && Math.Abs(position.x - 0.65 < 0.001
As part of my original post: "$$anonymous$$y issue is that I know that the elseif statement works, because I tested it by replacing gameOverVal++ with Destroy(gameObject) and, sure enough, the object was destroyed when those two conditions were met."
The issue is it's not updating gameOverVal++ if that's included, hence: "But it seems like it doesn't care for the increment I specified. What do?"
So this works: else if ((transform.position.y <= -4F) && transform.position.x == 0.65F) { Destroy(gameObject); }
And this doesn't: else if ((transform.position.y <= -4F) && transform.position.x == 0.65F) { gameOverVal++; }
Your answer
Follow this Question
Related Questions
I was trying to make my game in unity but I got stuck while making "game over" part. please help! 0 Answers
Rayscast from the GameObject that is not the player does not work 0 Answers
unity C# Errors `UnityEngine.Material' does not contain a definition for `SetColorArray' 0 Answers
How can I make a UI button press affect something in a text box? 0 Answers
UnityAction Vs UnityEvent 1 Answer