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 /
This question was closed Apr 19, 2018 at 06:57 PM by The2ndQuest for the following reason:

I had the wrong (but similarly-named) objects assigned to the script, code was fine.

avatar image
0
Question by The2ndQuest · Apr 19, 2018 at 08:36 AM · gameobjectbooleanif statement

Bool based on objects existing not changing.

While doing a tutorial exercise called "Chaos Ball" from a book, one aspect of the code was not triggering a bool that displays a GUI element (and my code is almost exactly the same as both the printed and example files, as far as i can tell- the only thing I've changed is the initial bool being set to "false" because the printed "true" didn't work and doesn't make sense to me if you're testing for it to be positive).

I've tested things with print statements to monitor the state of the bool and have tried other if statement approaches as an attempted work around (which just end up instantly making the bool true and triggering the GUI message right away instead of after the conditions are met that it's supposed to wait for). So, as far as I can tell, it's either the bool itself or the way the script references objects in regards to the bool. I'm just curious to figure out what's going wrong here or what I'm not understanding about the shorthand code being used.

The tutorial project is several colored balls bouncing around an arena which get destroyed when they collide with a matching goal color. The destruction and movement is controlled by other scripts. This script is just monitoring the state of the game to trigger the GUI message when all four colored balls are destroyed. (all four ball objects from the Hierarchy are assigned to their slots in the script in the Inspector as well).

 using UnityEngine;
 using System.Collections;
 
 public class GameControlScript11 : MonoBehaviour 
 {
 
     public GameObject blueBall, greenBall, redBall, orangeBall;
     private bool isGameOver=false;
     
  
     void Update () 
     {
         isGameOver = !blueBall && !greenBall && !redBall && !orangeBall; 
     }
 
     void OnGUI()
     {
         if (isGameOver) 
         {
                //the (working) code for displaying the message using GUI.Box & GUI.Label  goes here
         }
     }
 
 
 }
 
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

  • Sort: 
avatar image
0
Best Answer

Answer by Kciwsolb · Apr 19, 2018 at 03:21 PM

I put this code in a project to be sure, and isGameOver is being set properly. In other words, when all 4 balls are assigned in the inspector and the game is started, isGameOver is false, but when I delete the 4 balls from the scene, those references become null, and isGameOver changes to true.

So I am guess one of the following:

  • You didn't assign the balls in your scene to your GameObject variables (blueBall, greenBall, etc.) in this script. To do so, in the inspector drag and drop them from the hierarchy to the GameObject that has this script attached. I know you said you did this, but I have to mention it just to be sure.

  • Your ball GameObjects are not actually being destroyed by your other scripts. Make sure they aren't just being disabled.

  • Your GUI code in this script (which is not shown here) is not working properly. Maybe that is causing you to think isGameOver is not being set properly?

There is nothing technically wrong with line 13 in the code above. It does work properly. Again, I tested this out in a scene, and as long as the references are set up right in the inspector (which I know you said you did) then the bool isGameOver is being updated as expected. Again, you said you did, but try verifying this with a Debug.Log(isGameOver) statement in Update.

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 The2ndQuest · Apr 19, 2018 at 06:56 PM

Doh! You're right! I had the colored goal objects set to the script, not the colored ball objects. I'm an idiot! lol. Well, at least I'm not going insane trying to figure out what is wrong with the code. Thanks!

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

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI box open on MouseButtonDown 1 Answer

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

Unity crashes after I destroy a gameobject 1 Answer

Only delete the gameobject ONCE 2 Answers

How to recycle random prefabs that spawn from a specific point. 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