Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 IKarmaUK · Apr 02, 2017 at 10:04 AM · not workingif-statementsreturn

Why doesn't my if statement work?

I'm a little confused as to why my If statement isn't working.

Basically, I have a project set up so that I can open any of 3 scenes from a main menu, and then hit the 'escape' key to return to the main menu. I currently have it working on two of my scenes, but not on the third.

The code I'm adding the return feature to is a coin collector, where the user clicks coins to make them disappear. Here's a snippet from the update function:

 public void Update ()
     {
         if (Input.GetKeyUp(KeyCode.Escape))
         {
             SceneManager.LoadScene(0);
             MenuScript.ReturnOn();
             Debug.Log("Escape");
         }
 
         if (Input.GetKeyUp (KeyCode.Space))
         {
             int counter = 0;
 
             while (counter < 100)
             {
                 GameObject myClonedSprite = Instantiate(gameObject) as GameObject;
 
                 myClonedSprite.transform.Translate(0.5f, 0.5f, 0);
 
                 counter++;
             }
         }
     }

You can see the first 'if' statement is my return to menu. The problem is that when there is a coin (the GameObject in the code) in the scene, the escape key works fine. However, when there isn't a coin, then the escape key does nothing. I feel like it might be some clashing between the if statements, but I've tried replacing the return code, but nothing has worked. I'm fairly new to programming, so excuse my naivety.

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 tanoshimi · Apr 02, 2017 at 06:42 PM 0
Share

There's nothing obviously wrong with your script - the if statements are completely independent. You can prove this by removing the entire second block (from line 10-22). When you say "the escape key does nothing", you don't get the word "Escape" written to the console log? What does $$anonymous$$enuScript.ReturnOn do?

avatar image Commoble · Apr 02, 2017 at 06:59 PM 0
Share

In the situations where the escape key does nothing, is this script attached to an active object in the scene?

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by patrakus · Apr 02, 2017 at 07:25 PM

For me your if statemant are correctly writed, but why do you have public before Update()? Remove this and check if this helped.

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 Commoble · Apr 02, 2017 at 07:27 PM 1
Share

This won't solve this problem. $$anonymous$$essage events aren't required to be public due to the way unity invokes them, but setting them to be public doesn't prevent them from working (it just removes some protection and allows them to be called by things that shouldn't be calling them).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

If statement will not return true 3 Answers

Return Acting As Space(4.3.2) 0 Answers

Using bool function with parameters 1 Answer

My "if" is not working and have no idea why 1 Answer

Time is not stopping 2 Answers


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