So I have a script for an object that when shot explodes and it worked literally perfectly until I tried to add sound at which point it not only didn't play the sound but also stopped exploding, which was pretty rude of it.

Here's my code since I have no idea what's wrong: (Unity C#)

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

public class ShootyTarget : MonoBehaviour
    public float health;

    public GameObject destructionPrefab;

    public GameObject explosion;
    public GameObject shockwave;
    public GameObject debris;

    public AudioSource splodeSound;

    void Start()
        splodeSound = gameObject.GetComponent<AudioSource>();

    void FuckingDie()

        Instantiate(explosion, transform.position, transform.rotation);
        Instantiate(shockwave, transform.position, Quaternion.Euler(new Vector3(90, 0, 0)));
        Instantiate(destructionPrefab, transform.position, transform.rotation);
        Instantiate(debris, transform.position, transform.rotation);

    public void takedamage(float amount)
        health -= amount;
        if (health <= 0f)
  • 1
    \$\begingroup\$ Have you made sure the audiosource has a clip attached to it? It might cause a NullRef if not. \$\endgroup\$ – Panagiotis Iatrou Mar 13 '20 at 0:39
  • 1
    \$\begingroup\$ Do you get any error output in your console window? \$\endgroup\$ – DMGregory Mar 13 '20 at 1:18
  • \$\begingroup\$ The AudioSource has the right clip assigned to it and no errors are made. Sound still doesn't play \$\endgroup\$ – dumbass mcgee Mar 13 '20 at 1:33
  • \$\begingroup\$ The most likely cause here is if you forgot to put the AudioSource on the same object as this script. Then GetComponent returns null, so then the first line of the death function throws a null reference exception and aborts execution of the rest. If that's not what's happening, then you should attach a debugger and set a breakpoint in your takedamage function, so when taking lethal damage you can step through your code one line at a time until you find a line where the program does something different than what you expect. \$\endgroup\$ – DMGregory Mar 13 '20 at 11:55
  • 1
    \$\begingroup\$ Can you share a screenshot of your inspector pane, showing the full AudioSource settings? E.g. it's possible that the sound is fully spatial, but the volume falloff curve is short enough so as to cause a given distance to silence it. Also for reference, can you play other sounds normally, and can you confirm the splode sound sounds properly in e.g. Audacity? \$\endgroup\$ – Philipp Lenssen Mar 14 '20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.