- Home /
My "if" is not working and have no idea why
First of all I want to thank you guys. Reading the answers of the other questions, made a lot of things clear to me.
This is my problem:
I'm working on a 2D asteroid like game. There is a spaceship and it has a package connected (i'm using SringJoint2D to that).
When the player press "space" the package is disconnected:
if(Input.GetKeyDown(KeyCode.Space))
{
//SpringJoint2D
sj2d.enabled=false;
//this is just a bool variable to "record" if it's free
estaSolta=true;
}
There are two kinds of triggers that the package can collide. The right one (the package destiny) and the wrong one (the rest). When the package collides, it's supposed to show up a GUI menu. And this is the code i'm using to that:
void OnTriggerEnter2D(Collider2D col)
{
//if it collides with the right trigger
if(col.gameObject.tag=="destino")
{
if (estaSolta == true)
{
Time.timeScale=0f;
menuVitoria = true;
}
}
//if it collides with the wrong trigger
else
{
if (estaSolta == true)
{
Instantiate(explosao,transform.position, new Quaternion());
gameObject.SetActive(false);
menuDerrota=true;
Time.timeScale=0f;
}
}
}
void OnGUI()
{
//if it collides with the wrong trigger
if(menuDerrota)
{
//this is just an if to verify if the player selected Portuguese in the language menu
if(PlayerPrefs.GetInt("lingua")==1?true:false)
{
GUI.BeginGroup(new Rect(Screen.width/10, Screen.height/10, 400, 300));
GUI.Box(new Rect(0, 0, 400, 400), "Perdeu!!");
Screen.showCursor=true;
GUI.EndGroup();
}
//if it is in English
else
{
GUI.BeginGroup(new Rect(Screen.width/10, Screen.height/10, 400, 250));
GUI.Box(new Rect(0, 0, 400, 400), "You've lost");
Screen.showCursor=true;
GUI.EndGroup();
}
}
//if it collides with the right trigger
if(menuVitoria)
{
if(PlayerPrefs.GetInt("lingua")==1?true:false)
{
GUI.BeginGroup(new Rect(Screen.width/10, Screen.height/10, 400, 300));
GUI.Box(new Rect(0, 0, 400, 400),
"Meus parabens, voce conseseguiu fazer a entrega no prazo");
Screen.showCursor=true;
GUI.EndGroup();
}
else
{
GUI.BeginGroup(new Rect(Screen.width/10, Screen.height/10, 400, 250));
GUI.Box(new Rect(0, 0, 400, 400),
"Congratulations, you delivered on time");
Screen.showCursor=true;
GUI.EndGroup();
}
}
}
What is happening: When the package collides with the right trigger, the game freezes and the GUI menu appears but when the package collides with any other thing the game freezes, the bool variable menuDerrota becomes true but the GUI menu doesn't appear
can't test the script at the moment so I'm just guessing..
have you tried moving the following 2 lines around ?
menuDerrota=true;
Time.timeScale=0f;
to
Time.timeScale=0f;
menuDerrota=true;
don't think this will help but it's worth a try.. also, when you say the game freezes, you mean just the Time.timeScale, right? not like a freeze where you get some errors, because if you do, it would help if you post them as well :)
also some small comments on the code..
if(PlayerPrefs.GetInt("lingua")==1?true:false)
you can drop the "true:false" part as this doesn't really make much difference and just looks plain odd..
if(PlayerPrefs.GetInt("lingua")==1?true:false)
and
if(PlayerPrefs.GetInt("lingua") == 1)
will work the same way and 1 of them is a bit shorter and a lot easier to understand :)
usually you would use the
x == 1 ? .... : ...
in cases where you try to avoid writing
if(...)
PlayerPrefs.GetInt("lingua")==1
will return true or false either way so there is no reason to double check that value..
also, if you plan on using multiple languages or even just 2, it would probably save you some time if you would use a dataBase or some other form of check ins$$anonymous$$d of having a "if" for each language..
or just use a List or an Array.. and example would be and array where Portuguese is on position 0 and English on 1..
String[] grats = {"$$anonymous$$eus parabens, voce conseseguiu fazer a entrega no prazo","Congratulations, you delivered on time"}
just keep trakc of the PlayerPrefs.GetInt("lingua") value (which in this case should be either 0 or 1 and do something like
if(menuVitoria)
{
GUI.BeginGroup(new Rect(Screen.width/10, Screen.height/10, 400, 300));
GUI.Box(new Rect(0, 0, 400, 400),
grats[PlayerPrefs.GetInt("lingua")]);
Screen.showCursor=true;
GUI.EndGroup();
}
where grats[PlayerPrefs.GetInt("lingua")] will simply pick the text from the "grats" array depending on the value of PlayerPrefs.GetInt("lingua")
sorry if this didn't help a lot with your problem, but I just wanted to help imrpove your code a bit <_<
Thanks for your comments. I was not thinking to have more than two languages. But now, If I need to change to that, your comment will help me a lot.
Answer by HarshadK · Sep 30, 2014 at 06:22 AM
In your
if (estaSolta == true)
you are disabling the current game object by setting
gameObject.SetActive(false);
So once the game object is disabled none of the script attached to that game object will execute hence your remaining script is not executing and no GUI is shown.
One workaround will be to keep all your OnGUI code in a separate file on another game object and then even if you disable the current game object you can still show the GUI.
Also make sure you set your gameObject.SetActive(false) at the end of your else.
Thanks a lot. That worked perfectly. I made a GUIcontroler gameObject and set a bool from the script attached to the GUIcontroler to true. I was thinking that "SetActive(false)" was just like hide something