I am almost there!

Hello Team

I am almost there, however as I am adding audio to my reward.screen. I am facing a challenge and I thought I’d ask the community.

I have added a sound to be play when a reward.screen is on state. Everything works as expected, however when I go back to the mainmenu, the sound plays again on exit.

Should I look into seting up a bool, or is there a simpler anwser to this?

Thank you :hugs::hugs:for your help

Hi Damien,

How is your audio configured? e.g. how have you added it, did you add an AudioSource component to a GameObject and set its clip, then call it’s Play method from code? Or did you approach this in a different way?

Hi Rob

I have created a

//Sounds     
public AudioClip mario;

and attached the sound. Then I connected a sound component inside the inspector.

This is how I call the sound:

 //Sounds
    public AudioClip mario;

    void ShowLevelRewards()
    {
        switch (level)
        {
            case 1:
                AudioSource audio = GetComponent<AudioSource>();
                audio.PlayOneShot(mario);
                Terminal.WriteLine(@"
1 Like

Hi Damien,

Ok, so I don’t expect any PlayOnAwake or Looping settings on the AudioSource are the issue here, although you could check them to be sure.

It sounds as if your state is still true after the audio is played and then your logic allows for that to be repeated. It might be that you could set the state change immediately after the audio is played, or, as you suggest, perhaps use a bool to check against playing again, but I think the need to have that would indicate an issue elsewhere.

It’s been a while since I’ve looked at the Terminal Hacker project, from memory there was a Hacker.cs script that you create, could you copy/paste the whole contents into your post here so I can step through and see what happens when.


See also;

Hi Rob,

Thanks for your quick response.

So I find this quick work around, I guess just for the purpose of this Terminal Hacker game its more than enough.

I place a STOP, when I trigger the Input == menu :

                AudioSource audio = GetComponent<AudioSource>();
                audio.PlayOneShot(mario);
 void ShowWinScreen(string input)
    {
        currentScreen = Screen.Win;
        ShowLevelRewards();
        if (input == "menu")
        {
            AudioSource audio = GetComponent<AudioSource>();
            audio.Stop();
            ShowMainMenu("Hello " + yourName);
        }


    }

    void ShowLevelRewards()
    {
        switch (level)
        {
            case 1:
                AudioSource audio = GetComponent<AudioSource>();
                audio.PlayOneShot(mario);
                Debug.Log(mario);
                Terminal.WriteLine(@"

Glad you have found something that works for you Damien.

Note, if your code is all in the one class, you can improve it a little by not duplicating the GetComponent<AudioSource> calls you have, for example;

using UnityEngine;

public class Hacker : MonoBehaviour
{
    private AudioSource audioSource;    // member variable to hold reference to AudioSource component

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();    // get reference to AudioSource component (once)
    }
 
    // ...
}

In your existing methods, where you then declare a new variable to store the reference to the audio source, you can simply remove those lines, as you’ll already have a member variable, with a reference to the AudioSource component you can use;

 void ShowWinScreen(string input)
    {
        currentScreen = Screen.Win;
        ShowLevelRewards();
        if (input == "menu")
        {
            audioSource.Stop();     // use existing member variable
            ShowMainMenu("Hello " + yourName);
        }
    }
    void ShowLevelRewards()
    {
        switch (level)
        {
            case 1:
                audioSource.PlayOneShot(mario);    // use existing member variable
                Debug.Log(mario);
                Terminal.WriteLine(@" 

        // ...
    }

Thanks Rob, you’re right I could simplify the code. I tried but everything became RED and scary lol

I think this is something I will start focusing on in the next lesson.

regards
Damien

1 Like

hehe, no need to be scared. If you have an error in your code, often these can then cause a little cascade of errors, so often, correcting the issue near the top of a script will resolve the other issues that follow it.

If you would like to copy/paste your script with the change you made which caused some of the other code to be underlined/errored I am happy to take a quick look. Or, move on as you wish :slight_smile:

Privacy & Terms