- Home /
audio between scripts
I'm creating a clone of a missile and it uses audio for the launch and explosion. I don't think I quite have the hang of referencing other scripts, but here's my best shot. I have one script for the clone and one for the original missile G.O., that gets destroyed once I instantiate the clone. The scripts build but the audio won't work in the game.
here's the relevant code from the MissileClone script:
var detonator : Detonator = GetComponent(Detonator);
var newBehaviourScript :NewBehaviourScript;
var missileScript:MissileScript = GetComponent(MissileScript);
var audiO : AudioSource = missileScript.GetComponent(AudioSource);
function Start(){
audiO.clip = missileScript.audioClip1;
audiO.Play();
}
function OnCollisionEnter(collision : Collision)
{
detonator.Explode ();
rigidbody.isKinematic=true;
Destroy(GetComponent(MeshRenderer));
audiO.clip = missileScript.audioClip2;
audiO.Play();
Destroy(missileScript.target);
WaitForSeconds(6);
newBehaviourScript.cameraBack ();
}
And here's the relevant code from the MissileScript script: (this is all that's relevant)
var audio :AudioSource;
var audioClip1 :AudioClip;
var audioClip2 :AudioClip;
Answer by fafase · Aug 15, 2012 at 09:35 AM
I m a little confused with the missile and the clone. But let's give it a try.
Create a missile object with everything you need: mesh, texture, script. Then create a prefab and drag and drop the full model. The script on the missile may have a movement command. That is if it does not get a force. It has an audio source. that plays on awake with the sound of the fouiiiiiich.
Then it has a collision detection like you have on your question with an explosion sound as such:
AudioSource.PlayClipAtPoint(explosionAudio, positionOfCollision);
That will create an audio source object that destroys automatically on completion of the audio file independently of you missile already destroyed. On top of that the sound will come form the impact point, so if the collision is far away the sound is lower.
Now your guy might have something like:
var missile:GameObject;
function Update(){
if(Input.GetKeyDown(KeyCode.Space))Instantiate(missile, spawnPointPosition, rotation);
}
}
You drag and drop the prefab in the missile slot and that should work:
I would recommend to have a similar process for the explosion. Create a particle system for the explosion, Unity has one ready, and instantiate the particle on collision.
As I do not know your level of knowledge I might be telling you things you already know....
Actually, I'm only 17, haha! I've been using Unity as a segment of the ITTIP Digispired ii project, part of a government grant to see if $$anonymous$$ching kids about technology and program$$anonymous$$g gets kids interested in STE$$anonymous$$ (science, technology, engineering, math) fields. I can't believe I've gotten so far and I've only been using Unity for about a month! The forums and answer community have been a huge supplement to my learning.
Anyways, thanks! That's a big help. I didn't know about AudioSource.PlayClipAtPoint. That cleans it up nice. With the missile, that's basically what I'm doing, with the prefab and all, except I haven't officially made a prefab yet. I'm just using the one of the current missile G.O.s to instantiate a clone and destroy itself, because I want to test out the scripting first. Also, I'm using Unity's Detonator Package, so there's no need for the particles.
Your answer
Follow this Question
Related Questions
Assigning Default References to a script itself versus a GameObject using that script? 1 Answer
How to make click from menu and then appears in game (like Gmod?) or Minecraft 1 Answer
the most RESOURCE EFFICIENT way of referencing 1 Answer
Using classes between scripts in C#? 2 Answers
After update of Unity Editor all objects in scene lost references to any scene object. 2 Answers