• 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
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. T$$anonymous$$s 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

People who like this

0 Show 0
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

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 t$$anonymous$$s:

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, t$$anonymous$$s would be the way to handle it:

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

 // ...

}

Comment
Peter G

People who like this

1 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

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 t$$anonymous$$s:

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

People who like this

0 Show 0 · 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.

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

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

X is a variable but a type was expected 1 Answer

How to change variables on a new gameobject's script 1 Answer

Expression denotes a 'type' 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