- Home /
Faulty Code.
that code does'nt seem to want to work. what im trying to do is if otherObject is in view play sound. and if not. do nothing lol. seems to not want to work. a little help?var crashSound : AudioClip;
var otherObject : GameObject;
var soundplayed = false;
function Update(){
print ("Scary Noise");
if (otherObject.renderer.isVisible)
{
if(soundplayed==false)
{
Debug.LogWarning("Test sound");
otherObject.audio.PlayOneShot(crashSound);
soundplayed = true;
}
}
if (otherObject.renderer.isnotVisible)
{
soundplayed = false;
}
}
$$anonymous$$aybe the soundplayed = true part deactivates the if statement too soon and the sound can't be played completely - you could try this inside your if statement:
otherObject.audio.PlayOneShot(crashSound);
yield WaitForSeconds (crashSound.clip.length);
soundplayed = true;
In addition you should make sure that the otherObject has an AudioSource component attached.
Answer by idunlop_oefun · Jul 12, 2012 at 11:13 PM
if (otherObject.renderer.isnotVisible)
What is 'isnotVisible'? Does that compile?
Should it not be:
if (otherObject.renderer.isVisible == false)
Can you help me via skype... it takes like 6 hours per answer here...
Possibly - but can you do me a favor and answer the question.
isnotVisable does compile... but i dont think it works :I
also add me on skype $$anonymous$$iro.tygira
Can you post the complete script? I use C# so I'm not familiar with all of the JS syntax - however I'm puzzled that it compiles.
DON'T ask for help outside of UA. Unity Answers is a knowledge database. The questions and answers should help others as well. When you solve the issue somewhere else you cheat this community.
isnotVisible doesn't exist on the renderer component. You probably don't have a pragma strict at the top of your script, so it would compile almost everything, but that doesn't mean it works.
This is the correct answer. You have to either compare it to false or invert it, which is the most common way:
if (!otherObject.renderer.isVisible)
Note the "!". It's not a typing mistake, it's the logical negation operator which turns true into false and false into true.
btw. ever heard of "else"? ;)
if (otherObject.renderer.isVisible)
{
if(soundplayed==false)
{
// [...]
}
}
else
{
soundplayed = false;
}
Answer by cdarne · Jul 12, 2012 at 10:55 PM
Hi,
Maybe the otherObject GameObject has no "Audio Source" (or his volume is set to 0) or maybe the Camera Object has no "Audio Listener" (or it's deactivated)? Maybe you muted your computer's speakers ? (sounds obvious but it already happened to me...).
it does...
it wont load the "soundplayed = false;" at the end, so it wont repeat...
Your answer

Follow this Question
Related Questions
i want an object rotate 45 degrees more than an other one 1 Answer
Networking Gun Fire Sound 1 Answer
Why does my var speed say it is wrong 2 Answers
Parse music files for specific notes (DDR-style game) 2 Answers
Play sound when out of ammo 0 Answers