- Home /
Toggle Active GameObject State
Hey guys. I'm trying to work on a script for a sink that allows the player to toggle a prefab that is a child of the sink object (the water) I'm trying to make it so that when the player clicks the mouse, it toggles the water to simulate turning the sink on and off. However, I'm not quite sure how to do this. I tried setting it up but it won't work. Any help?
var AudioFile : AudioClip;
var object : GameObject;
var inRange : boolean;
var toggleBool : boolean;
function OnTriggerEnter() {
inRange = true;
}
function OnTriggerExit() {
inRange = false;
}
function OnMouseDown() {
if(inRange == true)
{
object.SetActive(toggleBool);
Switch();
Debug.Log("toggle");
}
}
function Switch() {
if(toggleBool == true)
{
toggleBool = false;
Debug.Log("set false");
}
if(toggleBool == false)
{
toggleBool = true;
Debug.Log("set true");
}
}
Answer by Eric5h5 · Aug 16, 2015 at 03:20 AM
You're setting the boolean to false, then checking if it's false (which it is because you just made it false), and setting back to true. However you don't need to do any of that, you can just set the state to the opposite value.
function OnMouseDown() {
if (inRange) {
object.SetActive (!object.activeInHierarchy);
Debug.Log ("toggle");
}
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Else and If 2 Answers
Pause Button Help 2 Answers
How to instantiate only once with boolean 1 Answer
Boolean wont acitvate if the enemy enters a trigger 1 Answer