• 9
• 9
• 11
• 13
• 9

This should be simple for someone to help me with (ADDING SOUND IN UNITY)

This topic is 912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm about to throw my computer through a wall. This is a very, very, simple game. The code below explains the balls health and when the ball gets below a certain level the audio will say "GAME OVER". The sound is not playing, I have no idea what else to do and I get no errors.

#pragma strict

var MaxFallDistance = -10;
private var IsRestarting = false;
var GameOver : AudioClip;

function Update ()
{
if (transform.position.y <= MaxFallDistance)
{
if (IsRestarting == false);
{
RestartLevel();
}
}
}

function RestartLevel ()
{
IsRestarting = true;
GetComponent.<AudioSource>().clip = GameOver;
GetComponent.<AudioSource>().Play();
yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
}

Share on other sites
I use C# instead of UnityScript, so it's liekly more clear to me what the problem is...

This:

yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);


causes the function to be converted to an iterator, or "coroutine" in Unity-speak.

Unity coroutines only function in two ways:

1. If the coroutine itself is a function that Unity calls directly, such as the MonoBehaviour.Start method.
or
2. If you call StartCoroutine on the function from a function within a MonoBehaviour.

Using them in other ways will not work properly because nothing will "pump" the coroutine.

You need to use #2 in this case. Change this line:

RestartLevel();

to this:

StartCoroutine(RestartLevel()); // This is what it would be in C# - refer to the Unityscript documentation for whether this is correct or not.

Edited by Nypyren

Share on other sites

if (IsRestarting == false);


And you have a stray semi-colon.

Share on other sites

if (IsRestarting == false);


And you have a stray semi-colon.

.... *insert crazy face here*  I am just sitting here shaking my head at myself... You sir/ma'am have helped me out a lot right now. I don't even know how to thank you because it seems so simple to you but as for someone starting out I am so happy lmfao. I have been doing this since last night and tried a million different ways and it was that one semi-colon.

Thank you to the other answer as well but the semi-colon being deleted worked.

Share on other sites

EDIT: maybe I didn't understand the code, nevermind :/

Share on other sites

It's always good to sprinkle a few debug.log() calls around your code that isn't working.  If I don't see the expected output, or I see output I shouldn't be, then I can pinpoint where things are going wrong.  (Or you can attach a debugger and walk through, but for Unity, I find the debug output is often less of a hassle as a first pass)

Share on other sites

If you find you're getting nowhere, don't start trying random things, as it won't do much good.

Instead, stop trying. Come back the next day, and aim to find the problem (rather than trying to fix the problem). Often the fresh look already makes the difference.

I always stop coding when I notice I start making silly mistakes. I could continue, but in "silly mistakes" mode, I likely break more than I want to.

Getting some food, and/or a good night sleep helps a lot more than continuing while half asleep.