- Home /
Question by
nathanvj · Nov 22, 2015 at 08:20 PM ·
gameobjectdisabling
Disable AudioZone
I made a sprite to turn the audio off (button), but the script doesn't work. I have problems with .active thing... Here's the script:
#pragma strict
var audioIsOn : Sprite;
var audioIsOff : Sprite;
var Audio_Source : GameObject;
function Start ()
{
Audio_Source = GameObject.Find("homeAudio");
}
function OnMouseDown()
{
if(Audio_Source.activeInHierarchy == true) //also tried .active and .activeSelf
{
Audio_Source.SetActive(false);
// also tried: .active = false
// activeSelf = false and activeInHierachy = false
GetComponent(SpriteRenderer).sprite = audioIsOff;
}
if(Audio_Source.activeInHierarchy == false) //also tried .active and .activeSelf
{
Audio_Source.SetActive(true);
// also tried: .active = true
// activeSelf = true and activeInHierachy = true
GetComponent(SpriteRenderer).sprite = audioIsOn;
}
}
Comment
Best Answer
Answer by F3RILLA · Nov 22, 2015 at 08:22 PM
Try this:
function OnMouseDown()
{
if(Audio_Source.activeInHierarchy == true) //also tried .active and .activeSelf
{
Audio_Source.SetActive(false);
// also tried: .active = false
// activeSelf = false and activeInHierachy = false
GetComponent(SpriteRenderer).sprite = audioIsOff;
}
else if(Audio_Source.activeInHierarchy == false) //also tried .active and .activeSelf
{
Audio_Source.SetActive(true);
// also tried: .active = true
// activeSelf = true and activeInHierachy = true
GetComponent(SpriteRenderer).sprite = audioIsOn;
}
}
Also, I always use .activeself to check if a gameobject is active or not ;)
Thank you, was stuck for literally an hour! This fixed it, awesome! :-)
Your answer
Follow this Question
Related Questions
How to prevent a script on a disabled object from firing? 0 Answers
How to set game objects as inactive via button 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
gameObject.setActive not working even though function gets triggered 1 Answer
Help, Missing Object 1 Answer