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();
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
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😍
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
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.
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?
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...
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, 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 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? :)
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
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
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
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
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.
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();
This comment saved me from leaving this series in between😅
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
@@tanmaysalaskar bruh same
Instablaster.
Yes,😃this thing was confusing me
Thank you this is perfect for my game jam! (also its called “co” “routine”)
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😍
Hey Sam, great tutorial, straight to the point! :)
Thanks :)
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
Same here, just cant figure out the problem tho.
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.
See the next video at 7:22 mins for the code found it after I figured it out. LOL
Now that's exactly what I want.
Thank you!
Liked the tutorial a lot ;)
Thanks
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?
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...
thank you so much
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?
It’s “=“ not “==“
The first is equaling the variable, the second is used as a boolean comparison
@@samyam thank you for the quick response and the assistance!
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.
Thanks for pointing that out! I must have edited it out by accident, my bad!
Is it possible to make it so that the music stops playing at a certain scene?
Yeah! You can always get the active scene SceneManager.GetActiveScene() or trigger the music to stop once a new scene has been loaded
How to control the volume of songs being played in another scene?
You can set it in the audiosource component or do it via script docs.unity3d.com/ScriptReference/AudioSource-volume.html
@@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? :)
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
Ur da best
My musicManagerInstance is not going blue
What do you mean?
@@samyam Mine either, and I cannot simplify that sentence. My musicManagerInstance within the code will not turn blue.
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
anyone can tell me how to change from one song to another in the same scene?
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
@@samyam wow, that was quick, thx :D ill start messing with the audiosource thingy
This is good and all, but doesn't address if your music has an intro
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
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.