Crash Effects SFX coding problem

Hello, Visual Code is telling me that “GetComponent<AudioSource” does not work in the current context, and I am pretty sure that I followed the steps correctly. I am not sure why this is happening and how to fix it.

“”"
using UnityEngine;
using UnityEngine.SceneManagement;

public class CrashDetector : MonoBehaviour
{
[SerializeField] float loadDelay = 0.5f;
[SerializeField] ParticleSystem crashEffect;
[SerializeField] AudioClip crashSFX;

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Ground")
    {
        crashEffect.Play();
        GetComponent<AudioSource>.PlayOneShot(crashSFX);
        Invoke("ReloadScene", loadDelay);
    }
}

void ReloadScene()
{
    SceneManager.LoadScene(0);
}

}
“”"

This is wrong. You are calling a method to ‘get the component of type AudioSource’. Because this is a method, it needs parentheses. It should be
GetComponent<AudioSource>().PlayOneShot(crashSFX);

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms