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).
public class ArrowCon: MonoBehaviour
void Start ()
this.player = GameObject.Find ("player");
void Update ()
transform.Translate (0, -0.1f, 0);
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
GetComponent (). Play ();// I want to make a sound
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).
GameObject director = GameObject.Find ("GameDirector");
(). DecreaseHp ();
(). Sound ();
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.
- c # - play sound effects using unity is trigger
- c # - object is not generated in unity sound game production
- unity c# i want to hear the contact sound between a ball and a block
- c # - unity 202010f1 + oculusrift hmd position tracking is not reflected in the game
- about programming basics unity c #
- c # - [unity ray cast] execute the function of the object that received the ray
- c # - [unity 2d] i want to create a gimmick that clings to a rope, hangs, swings like a pendulum, and jumps
- about debugging online games with unity
- unity - i want to use audiomixer because the sound effects playing in the barrage game playclipatpoint are broken
- c# - [unity] i want to stop only the specified sound (playoneshot)
- unity - design creation in 3d games
- c # - about player prefs for unity
- unable to publish unity games
- c # - unity) video thumbnails
- [unity c #] how to completely stop walking when switching panels
- how to use tolist (): and linq;in unity c #
- [c # unity] how to set arrey or dictionary in scriptableobject?
- c # - when controlling ui in unity, there are times when you can access an instance of an object and times when you cannot
- c # - how to autorun after burning unity to dvd
- c # - player production in unity (shooting game)
- c # - if the player is killed, i want the enemy to wait for a while and restart from the continuation unity
- c # - i want to get the number of characters with unity ui text
- c # - unity) firebase db crashes
- c # - ontriggerenter2d does not work
- c # - [unity] i want to start the variables of the script of multiple generated objects individually
- c # - correct/incorrect password gimmick that combines 4-digit numbers
- c # - [unity] in ios, i want to prevent the game from stopping when the notification center or control center is opened
- c # - i want to share variables from other scripts