Glitch Garden "level win" sound repeats

Heya. i’ve had this bug for a while, but hoped that it would get fixed in a lecture, but now im at the end and its still here, so im asking for help. my code is all the basic stuff from the Glitch Garden chapter. I have tried implementing “PlayOneShot” for the sound bit, but i couldnt get that to work either… (this is my first unity course, so still finding this hard to understand sometimes) ive recorded the behavior and taken a screenshot of the script, where i think the problem might reside.

https://youtu.be/rcRfDnM0Flw


hope somebody can help me, like if ive gotten alot of great help so far from the community :smiley:

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

1 Like

With what’s available the only thing I could suggest is to double check and make sure the play on awake box is unchecked for winlabel.

it wasn’t turned on :frowning:
ive looked through all the code from github, and all my code match Rick’s code on github. so the difference might be in the unity settings somewhere… im running “Unity 2019.1.7f1 (64-bit)” and ive downloaded the entire repo, and installed the version Rick used, to see whats different but i just cant figure it out… guess ill move on for now, and maybe return to redo this project at some point… :frowning:

I took another look at the code and I have two more suggestions the first would be to put win/loseLabel.setactive, into the awake method instead of start.

Second would be to set them as false in the inspector itself, at the very top of the inspector you’ll see a checkbox where you can turn it off at the beginning.

Hopefully one of those works.

Edit: I guess thats only one since they both do the same thing.

had a friend look at it, he made me put in a new bool.

public class LevelController : MonoBehaviour
{
    [SerializeField] GameObject winLabel;
    [SerializeField] GameObject loseLabel;
    int numberOfAttackers = 0;
    bool levelTimerFinished = false;
    [SerializeField] float waitToLoad = 2f;
    bool isGameOver = false;

    private void Start() {
        winLabel.SetActive(false);
        loseLabel.SetActive(false);
    }


    public void AttackerSpawned() {
        numberOfAttackers++;
    }

    public void AttackerKilled() {
        numberOfAttackers--;
        if (numberOfAttackers <= 0 && levelTimerFinished && !isGameOver) {
            isGameOver = true;
            StartCoroutine(HandleWinCondition());
        }
    }

    IEnumerator HandleWinCondition() {
        winLabel.SetActive(true);
        GetComponent<AudioSource>().Play();
        yield return new WaitForSeconds(waitToLoad);
        FindObjectOfType<LevelLoader>().LoadNextScene();
    }

    public void HandleLoseCondition() {
        loseLabel.SetActive(true);
        Time.timeScale = 0;
    }

    public void LevelTimerFinished() {
        levelTimerFinished = true;
        StopSpawners();

    }
    private void StopSpawners() {
        AttackerSpawner[] spawnerArray = FindObjectsOfType<AttackerSpawner>();
        foreach (AttackerSpawner spawner in spawnerArray) {
            spawner.StopSpawning();
        }
    }
}

that made it work.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms