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 metalmutha · Feb 27, 2013 at 01:51 AM · collisioncollidersoundchangeaudio source

Change Audio source on collision

if i have a 1st person controller and 4 cubes named cube1, cube2 , cube3, and cube4. each has attached a 3d audio file. I want the player to collide with cube1, stopping the the audio attached to cube1 and starting the audio attached to cube2 and then the same for the rest till cube4

how would i code this?!

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
1
Best Answer

Answer by SubatomicHero · Feb 27, 2013 at 09:10 AM

Hey there,

probably using the OnCollisionEnter() function. Here is what I would do in C#:

 public void OnCollisionEnter(Collision other)
 {
     // Assuming the cube names are cube1, 2 etc
     if (other.transform.name == "cube1")
     {
         other.transform.audio.Stop(); // Stop the sound
         GameObject cube2 = GameObject.Find("cube2");
         cube2.transform.audio.Play(); // Play the sound
     }
 }

Obviously you could make the cube1, cube2 public variables so that you can drag them into the script in the inspector, which may make things easier rather than finding the object everytime a collision occurs. All you need to do is add each other condition for the other cubes. You could also use a switch statement:

 switch(other.transform.name)
 {
     case "cube1":
     {
         // do the same stuff here
         break;
     }
     case "cube2":
     {
         // etc etc
         break;
     }
 }

I hope this helps! :D

Comment
Add comment · Show 5 · 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 metalmutha · Feb 27, 2013 at 01:32 PM 0
Share

Thanks for the reply! although i tried pasting the top code but i get compiler errors :S

avatar image SubatomicHero · Feb 27, 2013 at 01:38 PM 1
Share

what are the errors?

avatar image metalmutha · Feb 27, 2013 at 01:41 PM 0
Share

Assets/Change position sound.cs(1,13): error CS0116: A namespace can only contain types and namespace declarations

Assets/Change position sound.cs(2,1): error CS8025: Parsing error

avatar image SubatomicHero · Feb 27, 2013 at 01:44 PM 1
Share

Is the code I gave you on the script attached to the first person controller? Can you supply all the code so I can see it?

avatar image metalmutha · Feb 27, 2013 at 01:58 PM 0
Share

public void OnCollisionEnter(Collision other) { // Assu$$anonymous$$g the cube names are cube1, 2 etc if (other.transform.name == "cube1") { other.transform.audio.Stop(); // Stop the sound GameObject cube2 = GameObject.Find("cube2"); cube2.transform.audio.Play(); // Play the sound } }

i have it attached to cube 1. would i not just edit the code depending on the box it is attached to? it doesnt seem to like the 1st line? sorry im pretty new to unity and have used abit of Java, havent used C# before.

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

10 People are following this question.

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

Related Questions

Collider + multiple sounds script 1 Answer

How do I make a scene change when my Player collides with a item? 1 Answer

Making a sound play once on collision, can you fix my script? 2 Answers

How can I make my players change colors when they collide? 2 Answers

Volume Slow down and High problem 1 Answer


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