Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by herp johnson · Aug 15, 2012 at 08:21 AM · audioreferencebetween

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;
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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....

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image herp johnson · Aug 15, 2012 at 04:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges