- Home /
Sound won't start from "new" classes/objects
I sigh when I read the question, but after two days I must accept this is impossible for me to solve..
I have tried alot of things to start a sound, and in all those ways it works where it always worked. I used to have objects (prefabs) with audio sources that played their own sound, but now I have a SoundManager who plays the sound, successfully. But only from (the script) from the object where the sounds came from in the first place.
My SoundManager (empty object) has a script SoundManager:
public class SoundManager : MonoBehaviour
{
public AudioSource flatMovingSound;
public static SoundManager instance = null;
void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
}
public void PlayFlatMovingSound()
{
if (!flatMovingSound.isPlaying)
{
flatMovingSound.Play();
}
}
public void StopFlatMovingSound()
{
flatMovingSound.Stop();
}
}
In the SoundManager there is an audiosource with the mp3-file.
Now, this works from object A (I hear a sound): public class ZMovement : MonoBehaviour { [SerializeField] private readonly float speed = 50F; private Vector3 lastPosition;
void Update()
{
if (lastPosition != transform.localPosition)
{
SoundManager.instance.PlayFlatMovingSound();
}
lastPosition = transform.localPosition;
...............scripts for moving object
But when I do the exact same thing in another object, this object does move but there is no sound.
I also checked if my code reaches the SoundMananger.instance, and it does. But the IsPlaying is always false when the PlayFlatMovingSound is being triggered then anything else than the initial object.
What essential unity principal am I missing? I honestly checked ten guides for this and they al say thesame: it should work...
Your answer
Follow this Question
Related Questions
Can't get Sound working but music is fine 1 Answer
AudioClip calculates length wrong 1 Answer
Sound plays right at the beginning 2 Answers
How to add a specific sounds to pictures 0 Answers