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 Dogg · Aug 04, 2014 at 09:47 PM · audiochange

Change Music When GameObject is Grabbed?

Hello again for the 1000th time awesome people of Unity Answers, I have a question as always. I managed to get my music to changed to a different track when I grab a certain gameobject, however when I grab the gameobject again it changes back to the first music track. My question is, how can I stop it from changing back to the first track, and instead just restart the second track over? Example: Track 1 plays, grabs gameobject, Track 2 plays for 10 seconds, grabs the gameobject again, Track 2 replays starting at 0 seconds(the beginning). Here's my music and how it changes so far:

 public float defaultSpeed = 5f;
 public float maxSpeed = 10f;

 public AudioClip[] songs;
 AudioSource audio;

 public float coeffSpeedUp = 5f;
 public float coeffSpeedUpTime = 5f;
 
 // Use this for initialization
     void Start ()
     {
 
         audio = GetComponent<AudioSource> ();
 
         audio.clip = songs [0];
         audio.Play ();
 }
 
 public void SpeedUp()
     {
         Debug.Log( "SpeedUp()" );
         // Speed up the player
         speed = defaultSpeed * coeffSpeedUp;
 
         audio.Stop (); //pauses the Track 1
         audio.clip = songs [1]; //gets the Track 2
         audio.Play (); // plays Track 2
         
         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
         Invoke ("EndSpeedUp", coeffSpeedUpTime = 5 );
 
 }
 
 void EndSpeedUp()
     {
         //Changes how fast the player goes
         Debug.Log ("ending");
         speed = defaultSpeed; // back to normal
         renderer.material.color = Color.white;
     
         audio.Stop (); //stops the Track 2
         audio.clip = songs [0]; //Gets the Track 1
         audio.Play (); //Plays Track 1
     }
 }

I call the function SpeedUp() by using this script(note that this script is the gameobject I grab): http://pastebin.com/8JxXvDan As for the EndSpeedUp() function, it's called 5 seconds after the SpeedUp function.

I know inefficient code, don't judge me. Anyways thanks for reading and your time.

Comment
Add comment · Show 3
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 Nerevar · Aug 04, 2014 at 10:35 PM 0
Share

hello,

maybe it would be relevant to see how you trigger those 2 functions.

You could remember the reference of the object you grabed and check if it as changed to know which clip to play .

avatar image Dogg · Aug 04, 2014 at 11:14 PM 0
Share

"maybe it would be relevant to see how you trigger those 2 functions." Haha yeah I thought that before I posted this question, but I didn't think anyone would ask since most don't. I've updated my question, check it out when you have the time please.

"You could remember the reference of the object you grabed and check if it as changed to know which clip to play." I kinda understand what your talking about, but I'm unsure. I'll mess around and see if I can get it to work.

avatar image Dogg · Aug 05, 2014 at 04:46 AM 0
Share

Bump. Still need help. :D

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ironblock · Aug 05, 2014 at 07:36 AM

you just need an other check thats all somthing like

  if ( speedUpSound == true )
 {
 /// change sount track here
 speedIpSound = false;
 }











Comment
Add comment · Show 2 · 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 Dogg · Aug 05, 2014 at 07:58 AM 0
Share

Thanks a bunch it works!

avatar image ironblock · Aug 05, 2014 at 08:02 AM 0
Share

no problem at all :)

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Stuck on this! Please help me out. 1 Answer

Simple Timer 2 Answers

A node in a childnode? 1 Answer

Accelerometer Quick Question(Moving Up and Down?) 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