Loop Audio between Scenes and Add Sounds Effects - Unity Tutorial

Поділитися
Вставка
  • Опубліковано 29 жов 2024

КОМЕНТАРІ • 45

  • @samyam
    @samyam  3 роки тому +13

    I seem to have accidentally edited out the part where I get a reference to the AudioSource component in the PlayerController, make sure you declare at the top:
    private AudioSource audioSource;
    And then in Awake you can do:
    audioSource = GetComponent();

    • @tanmaysalaskar
      @tanmaysalaskar 3 роки тому +1

      This comment saved me from leaving this series in between😅

    • @samyam
      @samyam  3 роки тому

      You can have an array of audio clips and assign a clip to the audiosource at random
      using random.range (make sure to set the random.seed prior so it will be more random)
      So you can put the audio in the Resources folder then load them at runtime:
      docs.unity3d.com/ScriptReference/Resources.Load.html
      then when the player dies you can do something like:
      Random.seed = System.DateTime.Now.Millisecond;
      audioSource.clip = audios[Random.Range(0, audio_count)]
      docs.unity3d.com/ScriptReference/AudioSource-clip.html

    • @vortexvgm
      @vortexvgm 3 роки тому +1

      @@tanmaysalaskar bruh same

    • @matthewisaias9906
      @matthewisaias9906 3 роки тому

      Instablaster.

    • @CGRunLough
      @CGRunLough 2 роки тому

      Yes,😃this thing was confusing me

  • @griffinbrooks6748
    @griffinbrooks6748 2 роки тому

    Thank you this is perfect for my game jam! (also its called “co” “routine”)

  • @CGRunLough
    @CGRunLough 2 роки тому

    I have problem related multiple sounds effect and play on different different time i think now i have solution😃
    I will try to solve my problem today
    Thanks for your this video😍

  • @DanLucaz
    @DanLucaz 4 роки тому +1

    Hey Sam, great tutorial, straight to the point! :)

    • @samyam
      @samyam  4 роки тому +1

      Thanks :)

  • @siennafreyja5400
    @siennafreyja5400 3 роки тому +5

    I'm getting an error message saying object reference is required for the non-static field, method or property for both AudioSource.clip and AudioSource.play

    • @IAmScarab
      @IAmScarab 3 роки тому

      Same here, just cant figure out the problem tho.

    • @IAmScarab
      @IAmScarab 3 роки тому +2

      Sienna Freyja, Ive figured it out if its the same problem as mine.
      public class PlayerController : MonoBehaviour
      {
      [SerializeField] private float speed, jumpSpeed;
      [SerializeField] private LayerMask ground;
      private PlayerActionControls _playerActionsControls; // The "PlayerActionsControls" script assignment.
      private Rigidbody2D _rb;
      private Collider2D _col;
      private Animator _animator;
      private SpriteRenderer _spriteRenderer;
      private AudioClip deathSound;
      private AudioSource audioSource; // ADD THIS LINE OF CODE
      private void Awake()
      {
      // Instantiate the components need before thestart method is called.
      _playerActionsControls = new PlayerActionControls();
      _rb = GetComponent();
      _col = GetComponent();
      _animator = GetComponent();
      _spriteRenderer = GetComponent();
      audioSource = GetComponent(); // ADD THIS LINE OF CODE
      }
      adding the two lines of code made mine work.
      Good luck.

    • @IAmScarab
      @IAmScarab 3 роки тому

      See the next video at 7:22 mins for the code found it after I figured it out. LOL

  • @ayshahabib4970
    @ayshahabib4970 4 роки тому

    Now that's exactly what I want.

  • @netmajstorcom
    @netmajstorcom 3 роки тому

    Thank you!

  • @se-ku3qn
    @se-ku3qn 4 роки тому

    Liked the tutorial a lot ;)

  • @ErzxVLR
    @ErzxVLR 4 роки тому +1

    Thanks

  • @shionakasim9256
    @shionakasim9256 3 роки тому

    You are amazing! Your tutorials help me by alot, and also very clear in explanation!...could u perhaps do a video with IU buttons That open a new scene(s)? Or `IU buttons That Moves the character on a isometric Tilemap?

  • @leomassafm160
    @leomassafm160 Рік тому

    Great video! I've been having this problem where I want a song to loop, but the audio file has a second or two of pickup before the beat actually begins, and with every subsequent loop I want the audio to start right as the pickup ends. How do I do that? audioSource.time isn't really accurate...

  • @kelayran1189
    @kelayran1189 3 роки тому

    thank you so much

  • @hk-0148
    @hk-0148 Рік тому

    Hello, I am only using the music script for I am working on a prototype project for college, and I cannot get the script to work. The code in script:
    using UnityEngine;
    public class MusicManager : MonoBehaviour
    {
    private static MusicManager musicManagerInstance;
    void Awake()
    {
    DontDestroyOnLoad(this);
    if (musicManagerInstance == null) {
    musicManagerInstance == this;
    }
    else {
    Destroy(gameObject);
    }
    }
    }
    For line 13, musicManagerInstance == this; , an error pops up.
    The error states: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
    How can I fix this? Also, how come the "musicManagerInstance" stays white and does not turn blue like how it looks when you code it?

    • @samyam
      @samyam  Рік тому +1

      It’s “=“ not “==“
      The first is equaling the variable, the second is used as a boolean comparison

    • @hk-0148
      @hk-0148 Рік тому

      @@samyam thank you for the quick response and the assistance!

  • @IAmScarab
    @IAmScarab 3 роки тому +1

    samyam, I think you are missing a few steps when editing the video as I didn't see the declaration of the GetComponent() method in the Awake() method. Not sure if I had a similar experience in a prior video but could easily figure that one out. Create tuts tho nice a clear and upto date code.

    • @samyam
      @samyam  3 роки тому +1

      Thanks for pointing that out! I must have edited it out by accident, my bad!

  • @BeamerGaming33
    @BeamerGaming33 2 роки тому

    Is it possible to make it so that the music stops playing at a certain scene?

    • @samyam
      @samyam  2 роки тому

      Yeah! You can always get the active scene SceneManager.GetActiveScene() or trigger the music to stop once a new scene has been loaded

  • @vinizimba
    @vinizimba 4 роки тому +1

    How to control the volume of songs being played in another scene?

    • @samyam
      @samyam  4 роки тому +1

      You can set it in the audiosource component or do it via script docs.unity3d.com/ScriptReference/AudioSource-volume.html

    • @vinizimba
      @vinizimba 4 роки тому

      @@samyam I tried to change it by script but it doesn't recognize it in the current scene. I wanted to show you this in a video for you to understand. maybe that can become content for your videos. How can we do it? :)

    • @samyam
      @samyam  4 роки тому

      Vinicios Ferreira you won't be able to change content in a scene that isn't open, you would need to do it dynamically once the scene is loaded
      I also have a Discord which you can ask questions in the help channel discord.gg/bvQEGk7

  • @premmore2075
    @premmore2075 3 роки тому

    Ur da best

  • @hirozero146
    @hirozero146 2 роки тому +1

    My musicManagerInstance is not going blue

    • @samyam
      @samyam  2 роки тому

      What do you mean?

    • @hk-0148
      @hk-0148 Рік тому

      @@samyam Mine either, and I cannot simplify that sentence. My musicManagerInstance within the code will not turn blue.

  • @akaaidan5720
    @akaaidan5720 4 роки тому +1

    Great video again I have been binging your channel tryna create my own game if maybe my game gets big then we could work together as partners and split the profit

  • @ozmega
    @ozmega 4 роки тому

    anyone can tell me how to change from one song to another in the same scene?

    • @samyam
      @samyam  4 роки тому +2

      You can have multiple audio sources attached to a game object and get them by GetComponents() which returns an array
      You can also just have a reference to multiple Audio Clips and pass them into the audiosource
      audiosoure.clip = ;
      Then you can stop and play the different sounds. Here is the doc for it docs.unity3d.com/ScriptReference/AudioSource.html

    • @ozmega
      @ozmega 4 роки тому

      @@samyam wow, that was quick, thx :D ill start messing with the audiosource thingy

  • @linzenmeyer
    @linzenmeyer 2 роки тому

    This is good and all, but doesn't address if your music has an intro

  • @divinamaymedez7499
    @divinamaymedez7499 3 роки тому

    how can i stop the music and load another music for the final scene for highscore or anything?
    edit: I already solve my problem. i use gameobject.find to solve the problem

    • @samyam
      @samyam  3 роки тому

      GameObject find works but has to go through each gameobject until it finds it, if you can already store a reference to it it'd be easier and better. I personally think having a script managing the audio would be beneficial to keeping the code organized.