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 Keavon · Feb 05, 2011 at 06:42 PM · errorvariablesyntax-errorif-else

Error "There is already a local variable with the name 'obstacle'." How can I make it work?

Hello. This is the part of my script Unity hates. I can't assign "obstacle" more than one time. How can I fix this and let it work? Thank you!

if(whichToSpawn == 0){
    var obstacle = Ob_1;
}
else if(whichToSpawn == 1){
    var obstacle = Ob_1;
}
else if(whichToSpawn == 2){
    var obstacle = Ob_3;
}
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Feb 05, 2011 at 06:49 PM

Take off the declarations for the other two variables.

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 Keavon · Feb 05, 2011 at 06:52 PM 0
Share

Thank you, I actually did that just after I pushed publish question. Except I had to take off all 3. :D It works now!

avatar image Peter G · Feb 05, 2011 at 06:58 PM 0
Share

@$$anonymous$$eavon, js creates a global variable if you don't include the 'var' identifier.

avatar image Eric5h5 · Feb 05, 2011 at 07:02 PM 1
Share

@Peter G: that's not the case...'var' is just optional; nothing different happens if you use it or not.

avatar image Peter G · Feb 06, 2011 at 01:58 PM 0
Share

Hmm, I did not know that. Could you maybe explain what this tread is saying then...http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript

avatar image Eric5h5 · Feb 06, 2011 at 07:29 PM 0
Share

@Peter G: That's web Javascript; who cares about that? It's not relevant to Unity.

avatar image
0

Answer by Mai hime · Feb 05, 2011 at 06:56 PM

you have to create the variable obstacle once :

var obstacle=Ob_1;
    if(whichToSpawn == 0){
     obstacle = Ob_1;
}
else if(whichToSpawn == 1){
     obstacle = Ob_1;
}
else if(whichToSpawn == 2){
     obstacle = Ob_3;
}

Comment
Add comment · Show 4 · 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 Eric5h5 · Feb 05, 2011 at 07:03 PM 0
Share

In JS, because of the way it handles scope, you don't have to declare the variable ahead of time, you can just declare it for the first one.

avatar image Mai hime · Feb 05, 2011 at 07:10 PM 0
Share

even if it's after an if statment ?? i mean if the condition is false the code block inside if will not be executed

avatar image Eric5h5 · Feb 05, 2011 at 09:35 PM 0
Share

Yes, even if it's after an if statement. It doesn't matter if the code is executed or not; JS handles scope differently than C#.

avatar image Mai hime · Feb 05, 2011 at 10:55 PM 0
Share

thanks Eric for the inforamtion

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

No one has followed this question yet.

Related Questions

Unity Giving Errors after adding "Private" 1 Answer

What is the problem with this code? 1 Answer

Editing the jumppad from the 3rd person tutorial 2 Answers

I'm wondering with if function working 2 Answers

Save data not creating save file 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