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 · Dec 30, 2010 at 03:47 AM · errorvariableduplicate

If and else applying two states of a variable - complaining that the variable is duplicate?

Hello. This is an odd bug that I can't get to work. Here is my script:

var NormalBall : Rigidbody;

var BonusBall : Rigidbody;

function Start () {

}

function SpawnBall(){

var choice = Random.Range(1, 6);

if (choice == 1){

var TypeOfBall = BonusBall;

}

else {

var TypeOfBall = NormalBall;

}

var clone : Rigidbody;

clone = Instantiate(TypeOfBall, transform.position, transform.rotation);

}

My problem is that it gives me the error that the variable TypeOfBall is a duplicate,

Assets/Scripts/BallPart.js(11,5): BCE0067: There is already a local variable with the name 'TypeOfBall'.

Hmm. It also claims that

The prefab you want to instantiate is null.

I have it set in by dragging it into the box, yet it still says it's null. I would really appreciate some input, as I know perfectly well that it's a duplicate, and that's what I want.
Thank you!
-Keavon

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
1

Answer by Jessy · Dec 30, 2010 at 04:29 AM

No, you really should declare the variable with the var keyword; what you have is ambiguous. However, your entire function could be consolidated into this:

Instantiate(Random.Range(1, 6) == 1 ? bonusBall : normalBall);

You'll notice I renamed the variables; camelCase starting with lowercase for variables is the convention. UpperCase is for class, struct, and function names.

If you don't like the conditional operator, this would be the way to handle it:

private var typeOfBall; function SpawnBall() { if (Random.Range(1, 6) == 1) typeOfBall = bonusBall; else typeOfBall = normalBall;

 // ...

}

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 · Dec 30, 2010 at 05:10 AM 1
Share

There doesn't seem to be a reason to make that a global variable; I would keep it local. In JS you can declare the variable inline: "`if (Random.Range(1, 6) == 1) var typeOfBall = bonusBall; else typeOfBall = normalBall;`".

avatar image Jessy · Dec 30, 2010 at 05:29 AM 0
Share

I noticed that. It doesn't make any sense to me, why that works, so I didn't suggest it. I'd love a link about it.

avatar image Keavon · Dec 30, 2010 at 06:10 PM 0
Share

Are you saying that my origional script is global? I don't see anything global there. Thanks for the answers, but for simplicity's sake, I think I will keep it as it is, as it works fine.

avatar image Jessy · Dec 30, 2010 at 08:11 PM 0
Share

I disagree. Your code is very long, making looking through further lines of code a chore, and prohibiting quick comprehension. As for it being global, I don't know if that's the right term for it; Eric is saying that the way I showed you, the variable will be accessible outside of the function. And I'm saying I don't understand how his example compiles, even though I like it better.

avatar image
0

Answer by Keavon · Dec 30, 2010 at 04:24 AM

Oops. I found my mistake. The problem is that I wrote var before the TypeOfBall variable's, so it should be like this:

var NormalBall : Rigidbody;

var BonusBall : Rigidbody;

function Start () {

}

function SpawnBall(){

var choice = Random.Range(1, 6);

if (choice == 1){

TypeOfBall = BonusBall;

}

else {

TypeOfBall = NormalBall;

}

var clone : Rigidbody;

clone = Instantiate(TypeOfBall, transform.position, transform.rotation);

}

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

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

Assign prefab to variable through script 1 Answer

[4.6 UI] Show text as an int variable error 2 Answers

Can't import a variable from another script!! 1 Answer

error CS0841: A local variable `holdSound' cannot be used before it is declared 1 Answer

Weird error every frame 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