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 /
  • Help Room /
avatar image
0
Question by Pengawk · Feb 21, 2016 at 10:07 AM · c#game over

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?

Comment
Add comment
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

2 Replies

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

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;
      }

Comment
Add comment · 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
0

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
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 Pengawk · Feb 22, 2016 at 12:00 AM 0
Share

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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