Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ricardo Alves · Sep 30, 2014 at 04:53 AM · not workingif-statements

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

Comment
Add comment · Show 2
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
avatar image ShadoX · Sep 30, 2014 at 06:24 AM 0
Share

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 <_<

avatar image Ricardo Alves · Oct 01, 2014 at 03:25 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
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
avatar image Ricardo Alves · Oct 01, 2014 at 03:04 AM 0
Share

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

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

Why doesn't my if statement work? 1 Answer

Distance sensor 1 Answer

why != is different from var1 = !var2? 2 Answers

How to add two if statements to one result. (Flashlight being on, and pressing "F", to turn off the flashlight.) 3 Answers

Check Material C# 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