- Home /
2D Hiding mechanic troubles (Else statement not executing)
So I am trying to make a hiding mechanic, and this method called hide is called when you press e, but it does not work fully.
So when I press E the character vanishes, this is supposed to happen, then I press E again, but nothing happens, at one point I put a debug.log into the else statement, and I found that it never got put onto the console.
void hide()
{
if (Player.GetComponent<BoxCollider2D>().enabled == true)
{
Player.GetComponent<BoxCollider2D>().enabled = false;
Player.GetComponent<Rigidbody2D>().simulated = false;
Player.GetComponent<SpriteRenderer>().enabled = false;
}
else
{
Player.GetComponent<BoxCollider2D>().enabled = true;
Player.GetComponent<Rigidbody2D>().simulated = true;
Player.GetComponent<SpriteRenderer>().enabled = true;
}
}
anyone know whats going on here?
No way to see all, but is this without any doubt the same Player, AND the same BoxCollider2d?
Verify by setting a breakpoint at the start of hide when you expect enabled to be false, and inspect the values.
Come back with the results of that still puzzles.
I put in the breakpoint and found even that was not activated. No clue why, note that my code at this point looked like this, as I had tried some other things:
void hide()
{
if (Status == 0)
{
Status = 1;
Player.GetComponent<SpriteRenderer>().enabled = false;
Player.GetComponent<BoxCollider2D>().enabled = false;
Player.GetComponent<Rigidbody2D>().simulated = false;
Rigi = false;
Spi = false;
Box = false;
} //break point was never activated, so the else statement as a whole simply is not executing it seems.
else
{
Status = 0;
Player.GetComponent<Rigidbody2D>().simulated = true;
Player.GetComponent<SpriteRenderer>().enabled = true;
Player.GetComponent<BoxCollider2D>().enabled = true;
Rigi = true;
Spi = true;
Box = true;
}
}
So I put a breakpoint in the if statement, and found the variable status was 0, which was odd considering by the point the if statement breakpoint was activiated, I am fairly sure the number should've been a 1...
Next up, you need to find anywhere else Status might be changed to a zero behind your back ;)
What class owns the hide function?
Answer by $$anonymous$$ · Jul 11, 2018 at 02:53 PM
Okay, after recieving some help (Comments to my post here) those who were helping me sort this out came to this conclusion: Use OnTriggerEnter and Exit and not Stay. This seems to be working.
private int Status; //0 = nohide, 1 = hide
private void Start()
{
Status = 0;
}
public GameObject Player;
private void OnTriggerEnter2D(Collider2D other)
{
hide();
}
private void OnTriggerExit2D (Collider2D other)
{
hide();
}
private bool Rigi;
private bool Spi;
private bool Box;
void hide()
{
if (Status == 0)
{
Status = 1;
Player.GetComponent<SpriteRenderer>().enabled = false;
Spi = false;
Box = false;
} //break point was never activated, so the else statement as a whole simply is not executing it seems.
else
{
Status = 0;
Player.GetComponent<SpriteRenderer>().enabled = true;
Spi = true;
Box = true;
}
}
}
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 1 Answer
How come my camera does not follow my script? 1 Answer
Why simple letter evaluataion returns true no matter witch letter 1 Answer
Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers