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 · Jan 06, 2011 at 02:42 AM · errorinstantiatespawnclone

Is there anything wrong with this script? It used to work but now it doesn't.

Hello. Here is my code:

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);

}

It is supposed to randomly pick one of two balls to spawn and do so from a game object that this script is attached to. I have both balls set in that game object, but it is not spawning them. Any ideas? Thank you!

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
Best Answer

Answer by Craze · Jan 06, 2011 at 03:05 AM

lolol, Didn't notice that the start function is empty...? he isn't even calling it ROFL, no wonder it isn't working xDD

function Start () {
SpawnBall();
}

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 PrimeDerektive · Jan 06, 2011 at 02:57 AM

You are not declaring TypeOfBall anywhere... you probably want to put:

private var TypeOfBall : Rigidbody;

Up top with your other declared vars.

Also, try to reserve CamelCase for functions and methods, and use camelCase from variables and properties... its good practice.

Comment
Add comment · Show 2 · 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 · Jan 06, 2011 at 03:37 AM 0
Share

TypeOfBall is being declared, the first time it's used. "var" is optional, unless you use #pragma strict. You wouldn't want to make TypeOfBall a global variable for no reason, so the code is fine as-is, although personally I'd use var TypeOfBall = BonusBall; to make it more clear what's happening.

avatar image PrimeDerektive · Jan 06, 2011 at 03:50 AM 0
Share

Interesting, I didn't know that. I always read this line in the "head first into Unity with Javascript" page of the wiki: "The var keyword is required", without bothering to read the text under it: "In JavaScript, if you don't use the var keyword when defining a variable, that variable is scoped as a global one". Thanks!

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

Why my instantiate has error? 0 Answers

Spawn Script Issue 0 Answers

Code only affects first clone? 2 Answers

How can I assign a clone with a script to a variable? without drag & drop 1 Answer

SendMessage setName has no receiver! 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