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 Doozku · Jun 30, 2015 at 12:15 PM · javascriptvariablesglobal variablelocal variable

Unsure on how to access a var from another function

Hello, I have this code here that creates and destroys a main music system. The reason I want it to destroy the music is because in my level when I finish the level I want a special jingle to play but when the menu appears again (as of right now there is only one level) the music keeps playing. But now I get an error that says that the code is unsure of the variable I instantiated. How can I access the variable in another function?

This is the code of the stop music function.

  public static function StopAllAudio() {
         mManager.Destroy(AudioSource);
         for(var audioS : AudioSource in allAudioSources) {
             audioS.Stop();
         }
     } 

and this is the code of the create music function.

 function Start () 
 {
     currentScore = 0;
     
     if (!GameObject.FindGameObjectWithTag("MM")) {
         var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
         mManager.name = musicPrefab.name;
         DontDestroyOnLoad (mManager);
     }
 }

I want to access the stop music function from another script, so I can't pass the variable on. What do I do?

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 LaneFox · Jun 30, 2015 at 12:32 PM 0
Share

In public static function StopAllAudio() it doesn't seem to know what m$$anonymous$$anager or allAudioSources is. Have you defined these variables?

What console errors are you getting?

2 Replies

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

Answer by Doozku · Jul 03, 2015 at 09:16 PM

I found the answer. Apperently this works. I just declared a var and set it equal to the one in the program. Also I destroy it and make a new one in the new menu so it works. This is the new code.

 function Update () 
 {
     if (!GameObject.FindGameObjectWithTag("MM") && CreateMusic == true) {
         var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
         mManager.name = musicPrefab.name;
         if(mManager.GetComponent.<AudioSource>().enabled == false) {
             mManager.GetComponent.<AudioSource>().enabled = true;
         }    
         MusicManager = mManager;
     }
     //mManager.GetComponent.<AudioSource>().volume = MainMenu.Volume;
 }

 public static function StopAllAudio() {
     GameMaster.CreateMusic = false;
     GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
     for(var audioS : AudioSource in allAudioSources) {
         audioS.Stop();
     }
 }


and this is code from a different script that helped.

 function Update () 
 {
     if (!GameObject.FindGameObjectWithTag("MM") && CreateMusic == true) {
         var musicManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
         musicManager.name = musicPrefab.name;
         if(MainMenu.Volume) {
             musicManager.GetComponent.<AudioSource>().volume = MainMenu.Volume;
         }
         if(musicManager.GetComponent.<AudioSource>().enabled == false) {
             musicManager.GetComponent.<AudioSource>().enabled = true;
         }    
         musicMenu = musicManager;
     
     }
 }

Thank you all for helping.

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 hnmikechan · Jun 30, 2015 at 12:40 PM

In order for a variable to be seen by both methods you have to set the variable at a higher level. That way the variable isn't localized to a particular method and can be accessed by all methods. Also, I don't like this approach. I would just set up separate objects with audio sources, one for bg music and one for the menu to play the jingle. Then use audiosource.Stop() and audiosource.Start() in the appropriate order. I haven't used js for a while sorry, but try this for your current solution:

 var mManager;
 
 function Start () 
  {
      currentScore = 0;
      
      if (!GameObject.FindGameObjectWithTag("MM")) {
          mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
          mManager.name = musicPrefab.name;
          DontDestroyOnLoad (mManager);
      }
  }
 
 public static function StopAllAudio() {
          mManager.Destroy(GetComponent (AudioSource));
          for(var audioS : AudioSource in allAudioSources) {
              audioS.Stop();
      }
 } 
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 fafase · Jun 30, 2015 at 12:49 PM 0
Share

I am quite skeptical on that code.

First the declaration of the m$$anonymous$$anager has no type so it will be Object which makes it quite useless.

  m$$anonymous$$anager.Destroy(AudioSource);

This now is quite weird. Destroy is static so it won't be accessed from an instance. And the parameter should be a reference to an object while this is a type.

   "One cannot kill what does not exist."

And a type is not an object.

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

How to set a target position in Vector3.MoveTowards using a variable 2 Answers

Finding what scene number player is on and adding one.... 1 Answer

Creating new variables through incrementing 0 Answers

How do I load a scence upon clicking on and object? 1 Answer

Static Variables 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