• 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 LightSource · Jun 24, 2013 at 01:40 AM · errorvariableprivate

Unity Giving Errors after adding "Private"

Why does Unity want a } at 8/9 and want EOF at 22/9? All I did was add "private" to the variable. Thanks as always.

 var walls : GameObject;
 var animationStopped : boolean = false;
 var Mineshaft: AudioSource;
 var Halt: AudioSource;
 
 function Start () {
 
     private var aSources = GetComponents(AudioSource);
     
     Mineshaft = aSources[0];
     Halt = aSources[1];
     
     animation.Play();
     
     Mineshaft.Play();
 
     yield WaitForSeconds (animation["Elevator_Move"].length);
 
     animationStopped = true;
 
 
     }
     
 function Update () {
 
     if (animationStopped) {
         
         Mineshaft.Stop();
         Halt.Play();
         Destroy(walls);
         
         }
         
     }
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 AlucardJay · Jun 24, 2013 at 01:50 AM

You do not declare public or private for local variables.

Local variables only exist in the function they were declared in, and only for that frame. They cannot be seen or accessed by other functions.

There is absolutely no reason to mark a local variable as public/private, only Global variables :

 public var globalVariable1 : float;
 private var globalVariable2 : float;
 
 function DoStuff()
 {
     var localVariable : float;
 }
 
 function DoMoreStuff()
 {
     var localVariable : float; // same name, no errors, local variables only exist in the function they were created in, and only for that frame
 }
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 LightSource · Jun 24, 2013 at 02:10 AM 0
Share

Okay I understand. I assumed private served a different purpose.

Solved. But then I get these:

Array index is out of range.

Non matching Profiler.EndSample (BeginSample and EndSample count must match)

Ins$$anonymous$$d of starting a whole new question: any quick ideas?

avatar image AlucardJay · Jun 24, 2013 at 02:20 AM 1
Share

I've never put two audio sources on an object, I didn't think it was possible. I need to do some experimenting, but for what you're doing, consider just changing the audio clip on the audio source :

 @script RequireComponent(AudioSource)
 
 var walls : GameObject;
 var $$anonymous$$ineshaft : AudioClip;
 var Halt : AudioClip;
  
 function Start () 
 {
     animation.Play();
     
     audio.clip = $$anonymous$$ineshaft; // set the audio clip to $$anonymous$$ineshaft
     audio.Play();
      
     yield WaitForSeconds (animation["Elevator_$$anonymous$$ove"].length);
      
     animationStopped = true;
 }
  
 function Update () 
 {
     if (animationStopped) 
     {
         audio.Stop();
         audio.clip = Halt; // set the audio clip to Halt
         audio.Play();
 
         Destroy(walls);
     }
 }

I need to do some experimenting, to see if one can put two audio sources on an object, and if that's how you really want to do it.

Edit : you could do all this in Start :

 @script RequireComponent(AudioSource)
 
 var walls : GameObject;
 var animationStopped : boolean = false;
 var $$anonymous$$ineshaft : AudioClip;
 var Halt : AudioClip;
  
 function Start () 
 {
     animation.Play();
     
     audio.clip = $$anonymous$$ineshaft; // set the audio clip to $$anonymous$$ineshaft
     audio.Play();
      
     yield WaitForSeconds (animation["Elevator_$$anonymous$$ove"].length);
      
     audio.Stop();
     audio.clip = Halt; // set the audio clip to Halt
     audio.Play();
 
     Destroy(walls);
 }
avatar image LightSource · Jun 24, 2013 at 02:47 AM 0
Share

Perfect as always. This is a whole lot cleaner and I will be using this method in the future.

You can assign two audio sources by using the component menu in the inspector (Unity 4). Use the "audio" tab.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Can't access AsyncOperation instance to change allowSceneActivation 2 Answers

my animator controller does not work with a certain animation 1 Answer

Parser Error Unexpected symbol `private' 1 Answer

C# - public variable not exposed to the Project Panel? (n00b to c#) 1 Answer

Speedometer Error. PLS help ((( 0 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