Toggle/Switch objects and colliders on/off - Noob (artist) scripter

Calling all code Gurus,

I’m after a script that can toggle or switch a proposed (PTPropLift) and existing (ExistingMaster) object and all of its children, on/off using a gui button.

I am under the impression it may be easier to achive if there are two seperate buttons for an On and Off state?

The proposal needs to be deactivated at startup and the colliders also need to toggle on/off with the relative objects

Any ideas? This is way beond my coding expertise and I have already wasted hours on this one so would much appreciate any input

EDIT: Actually even if its just a button press rather than a gui, that would do! :slight_smile:

Please bear in mind - ABSOLUTE scripting virgin here

Cheers

I believe that you have a far simpler solution for this. In your editor. Make a few new Layers. Assign your object, (and all of its children when the popup comes up) to that layer, then simply turn the layer off to hide them all.

Or you could just use something like. Now they will start off deactivated and when you press “f” they will activate, then if you press “f” again they will deactivate and so on.

var PTPropLift : GameObject;
var ExistingMaster : GameObject;
var active : boolean;

function Start(){
PTPropLift.SetActiveRecursively(false);
ExistingMaster.SetActiveRecursively(false);
active = false;
}

function Update(){
if(Input.GetKeyDown("f")){
if(!active){
PTPropLift.SetActiveRecursively(true);
ExistingMaster.SetActiveRecursively(true);
active = true;
}
else{
PTPropLift.SetActiveRecursively(false);
ExistingMaster.SetActiveRecursively(false);
active = false;
}
}
}

Or, to build on that idea:

private var activate : boolean = false;

if (GUI.Button(Rect(10,70,50,30),"Click"))
{
activate = !activate;
PTPropLift.SetActiveRecursively (activate);
ExistingMaster.SetActiveRecursively(activate);
}

Yeeee Haaa it acually works!

One thing i di have to change was the “active” variable, gave me this message:

BCE0004: Ambiguous reference ‘active’: Toggle PT Proposals2.active, UnityEngine.Component.active.

Where Toggle PT Proposals2.active was the script name.

I stuck a capital on it to “Active” in all instances and it worked. No idea why just fluked it. Yup my coding skills are practicaly primeval!

Anyway many thanks guys, I salute you and your uncanny grasp of incomprehensible techno jargon! :slight_smile:

Glad you got it going. Just FYI, the reason “active” threw an error is probably because it’s a reserved word. Some words, like boolean, int, float etc. have a specific meaning in javascript or in this case Unityscript so they can’t be used as variable names. Not sure if that’s the case here, but could well be. You may have had a problem with the script name as well. Keep the script names simple and also different from any variable names that you’re using. I’ve managed to confuse Unity with that in the past.:smile: