#AudioSource.PlayClipAt is not playing when used in other function than Start() or Update()

1 messages · Page 1 of 1 (latest)

tepid mantle
#

When the asteroid collides with player, it's should make a hit sound, but never does.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AsteroidController : MonoBehaviour
{
    GameController gameController;
    [SerializeField] AudioClip hitSound;
    // Start is called before the first frame update
    void Start()
    {
        gameController = FindObjectOfType<GameController>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Destroyer")
        {
            gameController.AddScore();
        }
        else if(collision.tag == "PlayerShip")
        {
            AudioSource.PlayClipAtPoint(hitSound, this.transform.position, PlayerPrefs.GetFloat("MasterVolume"));
            gameController.GetDamage();
            Destroy(this.gameObject);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if(collision.tag == "Destroyer")
        {
            Destroy(this.gameObject);
        }
    }
}
north hemlock
#

it's not a static method, you need to tell it which AudioSource component you want to play the clip from

tepid mantle
#

PlayClipAtPoint is static

#

It's creating a new audio source just to play this sound and then automaticly destroys it

north hemlock
#

oh i guess i'm wrong, TIL there are static methods that play audio without an audiosource

tepid mantle
north hemlock
#

are you getting any errors? are you seeing the audio source being created? you aren't really providing any info

north hemlock
tepid mantle
north hemlock
#

are you certain that line of code is even running? you should also be using the CompareTag method to compare an object's tag rather than string equality

tepid mantle
#

When i put this line

AudioSource.PlayClipAtPoint(hitSound, this.transform.position, PlayerPrefs.GetFloat("MasterVolume"));

To Start() or Update() Functions then, it works, but not in Ontrigger

#

Rest of line is working, only audio isn;t

#

function*

north hemlock
#

calling it from OnTriggerEnter makes no difference from calling it from Start so that's not the problem

north hemlock
tepid mantle
#

Because I've added now the line with audio, and everything else was working before and after

#

Does 2
gameController.GetDamage();
Destroy(this.gameObject);
Are working fine, just no audio

north hemlock
#

put a debug.log in there to make sure it is running like you expect

tepid mantle
#

Where?

north hemlock
#

inside the if statement where you are calling PlayClipAtPoint

tepid mantle
#

But man, those 2 lines

gameController.GetDamage();
Destroy(this.gameObject);

Are in the same if statment, and they are working

north hemlock
#

and you are 100% certain that whatever colliding object isn't performing those tasks?

tepid mantle
#

Every Asteroid that hit the ship is disappearing so im 100% sure that Destroy function is working properly

#

And UI show lower ship HP with every hit

#

So both are working 100%

north hemlock
#

well that didn't answer the question i asked. can you just humor me and put a debug.log where i asked you to so you can actually confirm that the code is being executed properly instead of making assumptions about what you think is happening?

tepid mantle
#
else if(collision.tag == "PlayerShip")
        {
            Debug.log("Before audio");
            AudioSource.PlayClipAtPoint(hitSound, this.transform.position, PlayerPrefs.GetFloat("MasterVolume"));
            Debug.Log("After audio");
            gameController.GetDamage();
            Destroy(this.gameObject);
        }

This will be fine for you?

north hemlock
#

sure

tepid mantle
north hemlock
#

there we go, we actually confirmed the code is being executed. now i don't have unity open to check but does the PlayClipAtPoint method happen to make the audiosource a child of the object that calls it? i would generally assume no, but we should not be making assumptions, especially during debugging

tepid mantle
#

I knew that it was working without thos logs but ok

#

The PlayClipAtPoint is not making it as a child of asteroid

#

It's standalone gameobject