Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RolfBertram-com · Aug 22, 2011 at 08:02 PM · musicvolume

Changing Music Volume = Calling a function in another script in another scene

Music plays in all scenes, but I can't change the volume, except in the startup scene. I'm using the following JS to start the music in the startup screen of the game. The volume needs to be adjusted in the game's setup scene. Just saying "audio.volume =" works here in the startup script, but does not work in the later setup scene. Obviously the compiler does not know which volume I want to change, and ignores my request. Wasn't there a way to access, in this case, my function "MusicVolume" inside this script, which I named "BackgroundMusic", from another JS in another scene? I just don't know how to do this!

 var backgroundMusic : AudioClip;
 
     audio.clip = backgroundMusic;
     audio.Play();
     
     function Start()
     {
     MusicVolume();
     //if(!PlayerPrefs.HasKey("MusicLevel")) { audio.volume = PlayerPrefs.GetFloat("MusicLevel");}
     }
     
     function MusicVolume()
     {
     if(!PlayerPrefs.HasKey("MusicLevel")) { audio.volume = PlayerPrefs.GetFloat("MusicLevel");}
     }
Comment
Add comment · Show 1
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 RolfBertram-com · Aug 23, 2011 at 10:12 PM 0
Share

Thanks to those who answered. However, the problem appears to be more complex than expected. Decided to circumvent the problem by not using DoNotDestroy, and ins$$anonymous$$d playing the music only in the game scene. It will of cause stop when leaving the scene, and restart from the beginning when re-entering the scene.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaminghairball · Aug 22, 2011 at 08:07 PM

Given that it's a global music player, you'll probably want to make it a static thing, and make MusicVolume() a static function.

Comment
Add comment · 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
0

Answer by aldonaletto · Aug 22, 2011 at 08:55 PM

You must find the object at which the script is attached then access the script with GetComponent(BackgroundMusic). Supposing the object name is "Jukebox", you can do the following:

    var juke = GameObject.Find("Jukebox"); // find the object
    juke.transform.GetComponent(BackgroundMusic).MusicVolume();
But if you want to allow volume control at any point of your game, add this slide control to your script BackgroundMusic.js - it will show the slider all the time:

private var curVolume: float;

function Start(){ MusicVolume(); // set the initial volume from PlayerPrefs curVolume = audio.volume; // copy it to curVolume }

function OnGUI(){ // this slider will be shown all the time in the screen curVolume = GUI.HorizontalSlider (Rect (25, 25, 100, 30), curVolume, 0.0, 1.0); audio.volume = curVolume; }

Comment
Add comment · Show 3 · 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 RolfBertram-com · Aug 22, 2011 at 11:28 PM 0
Share

Thanks for your examples. However, I get a NullReferenceExeption Error at juke.transform.GetComponent(Background$$anonymous$$usic).$$anonymous$$usicVolume(); when running the script. Any idea whats wrong?

avatar image RolfBertram-com · Aug 22, 2011 at 11:58 PM 0
Share

Seems the object can't be found. Using this code: var juke = GameObject.Find("Jukebox"); // find the object if (juke == null) {print("No object found with that name!");} else {print("someObject name: " + juke.name); I get "No object found with that name!" The object is called "Jukebox", so it does exist. Why can't it be found?

avatar image aldonaletto · Aug 23, 2011 at 09:08 AM 0
Share

It's very strange: GameObject.Find finds any active object with the specified name, even if it's childed to other objects (Transform.Find doesn't). The only reason I can think for this to fail is this code being used at Awake - Awake occurs during object initialization, thus other objects may not exist yet. If this is the case, move the code to Start.
Anyway, you can place a GameObject variable in the script where the volume control is and drag your music object to it - something like:

var jukeBox: GameObject; // drag your jukebox here

function SetVolume(vol: float){ jukeBox.audio.volume = vol; // set directly the jukebox volume }

function ResetVolume(){ // restore the PlayerPrefs volume jukeBox.transform.GetComponent(Background$$anonymous$$usic).$$anonymous$$usicVolume(); }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

adjust the slider to control the volume 1 Answer

Multiple, independent volume sliders 1 Answer

Change volume of objects with a tag 1 Answer

Need help with my slider 3 Answers

Music Volume 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