Hey! So i’m facing a small problem in Unity, and i’m not too sure how to describe it, so i’ll try my best. So what I have is a gun, and then that gun shoots, if it hits an object with a tag, it’ll play a bullet impact sound for that tag. Instead of having all of the audio stuff in the same script as the weapon shooting script, I want to have them in a separate script that is for bullet impact audio. So in my scene I have an empty object called WorldAudio, which has empty objects inside it like Bullet_Impact_Wood, which contain audio sources. For the raycast shooting for the gun, this is the script I have:
function createBulletHoles() {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
var direction : Vector3 = transform.forward;
if (isAds) {
direction.x += 0.05;
direction.y += 0.05;
direction.z += 0.05;
} else {
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
}
if (Physics.Raycast (transform.position, direction, hit, 10000)) {
hit.transform.SendMessage("WorldAudio", hit.transform.tag, SendMessageOptions.DontRequireReceiver);
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var instantiated = Instantiate(bulletHole, hit.point, hitRotation);
Destroy(instantiated, 15);
}
}
the script also instantiates bullet holes where the raycast hits, and has a small spread to the raycast. So I have a raycast send message line, which sends the tag of the object to a function called WorldAudio. If that function is linked to the object that it hits, it works, but I want to have the shooting script send a message to the worldaudio object which has a script attached to it to tell it that an object was hit, and that it should play the audio for that object, but it doesn’t work. Anyone understand what i’m trying to say? Know the solution to it?