- Home /
I had the wrong (but similarly-named) objects assigned to the script, code was fine.
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
}
}
}
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.
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!
Follow this Question
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