Help with static functions and sound (C#)

I spent lots of time trying to figure out how to add multiple death sounds onto my enemy. I’m not sure if it worked or not because when it died, the game object was destroyed and the sound stopped playing.

I made an empty object with this script:

using UnityEngine;
using System.Collections;

public class enemysound : MonoBehaviour {
	
	public AudioClip boom3;

	public static void boom(){
		audio.PlayOneShot(boom3, 1.0F);
		audio.Play();
	}
}

Right before death, the enemy calls the boom function on the empty object with enemysound.boom(); But no matter how I try and play the sound I get error CS0120 on the line of audio.Play(); and same goes if I use Audio.PlayOneShot.

I haven’t been able to find any information on static functions, so any help is greatly appreciated.

Short and dirty answer: add the keyword “static” to the boom3 member variable declaration (thus no-longer making it a member). So “public static AudioClip boom3;”

I’m guessing you just want to get it working, but FYI static members/functions are almost never the right answer when it comes to creating quality software. They do have their uses for sure, but I always tell newcomers to avoid them completely.

More on static in C#