In a game I'm working on in Unity, I want to make a sound when an arrow hits a player, but when I run the script below, there is no sound or error.
In addition, I have unchecked Play on Awake, but when I check it, sound is produced (although the timing at which I want to produce sound is different).
I thought that there might be no sound because the object was destroyed, so I commented out Destroy (gameObject) ;, but there was still no sound.
The other instructions in this script are fine (such as destroying arrows).
Sounds attached to other objects (such as BGM) are produced normally (though they are produced by Play on Awake, not from the script).

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

public class ArrowCon: MonoBehaviour
GameObject player;

void Start ()
this.player = GameObject.Find ("player");

void Update ()
transform.Translate (0, -0.1f, 0);

if (transform.position.y<-5.0f)
Destroy (gameObject);

Vector2 p1 = transform.position;// Center coordinates of the arrow
Vector2 p2 = this.player.transform.position;// Player center coordinates
Vector2 dir = p1 --p2;
float d = dir.magnitude;
float r1 = 0.5f;// radius of the arrow
float r2 = 1.0f;// Player radius

if (d {
GetComponent (). Play ();// I want to make a sound
Destroy (gameObject);

  • Answer # 1

    As a result of various trials, I wrote a method to make a sound in a script attached to another existing object, and succeeded in making a sound by calling it when hit from the arrow script.
    This is an arrow script (excerpt).
    if (d {
    GameObject director = GameObject.Find ("GameDirector");
    director.GetComponent(). DecreaseHp ();
    director.GetComponent(). Sound ();
    Destroy (gameObject);

    This is the called script (excerpt).
    public void Sound ()
    GetComponent (). Play ();

  • Answer # 2

    GetComponent  (). Play ();// I want to make a sound
    Destroy (gameObject);

    If you delete it immediately after playback, there will be no sound.

    I'm sorry, I overlooked the question text.

  • Answer # 3

    It hasn't been completely resolved, but it started to make noise.
    The first thing I tried was to comment out not only the bottom Destroy, but the top Destroy as well.
    Then it started to make noise, but as a new problem, the arrows were no longer destroyed.
    The next thing I tried was to change the if (transform.position.y<-5.0f) on the Destory to if (transform.position.y<-30.0f) to delay the destruction.
    Now you can hear the sound and destroy the arrows (Destroy at the bottom remains commented out all the time).
    Last but not least, is it impossible for an arrow to be destroyed and make a sound when it hits the player?

    Probably because the arrow was not destroyed by the collision, but only one HP UI (I omitted it because it is unnecessary for the question script) that should have been reduced by 0.1f due to the collision with one arrow. It came to be scraped all by.
    Probably, I think that it keeps decreasing by 0.1f all the time while hitting.