Hi,
Which version of Unity do you use?
Hi,
Which version of Unity do you use?
2019.3.0a7
Apparently!
Thanks for checking this out.
Please do not use an alpha or beta version of Unity for your actual projects. They are full of bugs. Downgrade to a stable version with an “f” in its version number.
OH! How on earth did I end up with a beta. Thanks so much!
I have re-imported my scenes and assets, but unfortunately the behaviour is the same. I am still thinking the audioSource.Stop is killing the others sounds every frame. Any other ideas?
Thanks
Why can’t I be more specific about which audio to stop?
I was thinking something like this:
audioSource.Stop(mainEngine);
or
mainEngine.stop();
But it does not like either of those.
Is mainEngine
the AudioSource playing a clip you want to stop? If so, mainEngine.Stop()
should affect this AudioSource only, not the other ones.
Ahh, Interesting, I thought that seemed like a sensible thing to try, but when I do
mainEngine.Stop();
I get a error underline which says ‘AudioClip does not contain a definition for Stop’
I just tried again and got the same result.
Odd.
This means that mainEngine
is of type AudioClip, not of type AudioSource. Call Stop on the AudioSource object.
Yeah this is how it was setup originally, yet it kills all the audio sources, not just the sound at hand. It must be a code order/placement issue somehow…
Could you share your current code here, please? And what other AudioSources get stopped? In your script on pastebin, there is only one AudioSource.
See also:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour
{
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip success;
[SerializeField] AudioClip death;
Rigidbody rigidBody;
AudioSource audioSource;
enum State { Alive, Dying, Transcending }
State state = State.Alive;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.Alive)
RespondToRotateInput();
RespondToThrustInput();
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) {return;}
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
Invoke("LoadNextScene", 1f); //paramaterize this
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
Invoke("StartAgain", 1f);
}
private void StartAgain()
{
SceneManager.LoadScene(0);
}
private void LoadNextScene()
{
SceneManager.LoadScene(1);
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
{
ApplyThrust();
}
else
{
audioSource.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true; // take manual control
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false;
}
}
If I comment out the else section here, All the sounds work, but the thruster does not stop when the spacebar is released.
I am comparing my code to this lecture state https://github.com/CompleteUnityDeveloper2/3_Project_Boost/blob/2213cedfb9347b2891f8bbb156a29c02b67f00a8/Assets/Rocket.cs
Thank you.
And what other AudioSources get stopped? In your script, there is only one AudioSource object. Its reference is assigned to audioSource
.
This is one example of the ‘death’ sound earlier in the script.
No, that’s the same AudioSource object. The sound clip is not the AudioSource, and an AudioSource can play only one clip at once.
When you load a new scene, all game objects in the scene, including all AudioSources get destroyed. Since Invoke invokes the method 1 seconds later, it’s likely that your sound does not play as expected because the AudioSource gets destroyed along with the other game objects in the scene.
I am not entirely sure which end to attack this problem from.
Boost Re-imported.zip (843.6 KB)
It seems odd that I can get the behaviour almost working by removing the else statement at line 83-86 in rocket.cs, and without this message the sounds play, so to my best logic deduction, surely this is what is stopping them. If this audioSource.Stop(); really is just local, then why is it interfering with the other sounds.
It would appear that my code is functionally a mirror of the lesson example yet does not exhibit the same behaviour, thus either my code is different to the lesson example or the lesson example is somehow outdated, which I feel is unlikely considering the basic nature and the fact that no one else seems to have this problem.
What do you mean by local? If you listen to the radio (the only radio in the room!) and switch it off, it won’t play any music/sounds anymore. It does not matter if they are playing your favourite songs. Off is off.
and the fact that no one else seems to have this problem
Have you already downloaded Rick’s project and tested it? It might be that the percived bug is no bug but simply how Unity works.
Ok, thankyou so much for your advice and patience.
Since it was a minor bug, I decided to plow on with the particles to see what else I can discover. The particles (related to space bar) were not working as expected. So now I was sure I had broken it.
Having worked through the possibilities (downloading and checking Ricks proved it was not the system, it became obvious it was very much a user error.
I had missed a pair of {} after the ‘if (state == State.Alive)’
The joy of working sound and particles has overwhelmed me.
Thanks again!! Perhaps I can make it through this!!!
Oh, the good old missing curly brackets. That’s a very common problem which happens to everybody from time to time. I’m glad you spotted the problem.
Next time, add a bunch of Debug.Logs to your code to see if methods get executed as expected. If a a code block which is not supposed to be executed under certain circumstances, does get executed, it’s almost always a problem with an if-condition: either missing curly brackets, the condition is wrong or the something the condition checks is wrong. Debug.Logs helps you narrow down the problem.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.