x


Help With Alternating Booleans On Child Objects

Pretty new to scripting for games, but I've been getting better everyday. However, one script I've been working on has had me stumped for almost a week now.

The scenario, I've got two boxes in my scene that act like buttons. Starting out the red box should be the only one allowed to go from grey to red. After the red box is clicked, then the green box can be clicked to turn it from grey to green. I'm not wanting a radical script change that does only this, because this is a beta script for what will eventually be used with more "buttons" and a more complex boolean true/false pattern.

The largest problem I faced with this script, is that it does alternate booleans like I want, but not on other object. I put the script on both the green and red boxes, but when I click the red box, the booleans on the green box don't change, so I figure to fix this I'd have to put the script on an empty GameObject, then make these two boxes the children of the empty GameObject. My problem is that once I put the script on the empty GameObject, it doesn't know to change the booleans on the child objects when I click on them individually.

var GreenBox : GameObject;
var RedBox : GameObject;

var AllowedRed : boolean = false;
var AllowedGreen : boolean = true;

function Start() {
    AllowedGreen = false;
    AllowedRed = true;
    }

//----------Switches the Boxes Click Ability----------
function OnMouseDown() {
    if (GreenBox){
       if (AllowedGreen == true){
       AllowedGreen = false;
       AllowedRed = true;
         ColorGreen();}}

    if (RedBox){
       if (AllowedRed == true){
       AllowedRed = false;
       AllowedGreen = true;
         ColorRed();}}
         }

//----------Changes the Color of the Boxes----------    
function ColorGreen(){ 
if (GreenBox){
    renderer.material.color = Color.green;}}

function ColorRed(){ 
if (RedBox){
    renderer.material.color = Color.red;}}

Any help would be greatly appreciated, I've been stuck on this for about a week and some folks at the technology center are wanting to see a working script.

more ▼

asked Apr 09 '12 at 09:47 PM

KitWeiss gravatar image

KitWeiss
0 2 4

Will there be some sort of sequence pattern relation between these buttons later on? It might be essential to know how many there'll be and you might be better off with an array of buttons and booleans connected to them.

Apr 09 '12 at 09:52 PM save

Well there's going to be 4 buttons on the test objects I have. An elevator with a "Close" "Open" "Call" and "Hide", but I'm wanting to be able to apply this script to multiple, similar setups. Like, at the top of the script I have to "var"s where I can drag and drop an object into in Unity. Really the pattern is determined by which buttons I allow to be pressed when another button has been pressed first. Like, when you click the "Open" button on the elevator, I don't want the player to "Call", "Open", or "Hide" (which sends the elevator down), and when they click "Close", then they'll be able to "Hide". Etc.

What's got me stuck is how to put this script on an empty parent object, and have it kind of "listen" to the two objects I've defined. The GreenBox and RedBox will be children of the empty parent.

Apr 09 '12 at 10:15 PM KitWeiss
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x427
x326
x18

asked: Apr 09 '12 at 09:47 PM

Seen: 282 times

Last Updated: Apr 09 '12 at 10:34 PM