• 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 Muzz 1 · Mar 23, 2011 at 08:15 PM · javascriptvariablemathmaths

Maths with variables

I'm trying to get it to instantiate an object every time a ball hits a bucket, and when there are no more balls left to instantiate something else. Here is the script...

var total : float = 2; var firework : GameObject; var celebration : GameObject;

function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.name == "Bucket"){ print (total--); var instance : GameObject = Instantiate(firework, transform.position, transform.rotation);

} }

function Update(){ if (total = 0); var instance : GameObject = Instantiate(celebration, transform.position, transform.rotation); }

Any ideas?

Comment
Add comment · Show 1
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 Muzz 1 · Mar 24, 2011 at 05:36 PM 0
Share

One problem now, is that the celebration instantiates when ONE ball hits the bucket. (e.g. when total == 1). I want it to be after both (obviously.) I've moved it to the OnCollisionEnter section, and sorted out all the other problems.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DaveA · Mar 23, 2011 at 09:22 PM

if (total = 0);

You probably want

if (total == 0) two = signs, no semicolon

When you fix that, it will instantiate one of those things every frame as long as total == 0, which is probably NOT what you want. You should instantiate when the determination is made, or otherwise set a boolean variable or something saying that you have instantiated that object, so as to avoid doing it again every frame.

Check that the let us know

Comment
Add comment · Show 7 · 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 Muzz 1 · Mar 23, 2011 at 09:24 PM 0
Share

Of course... I feel so stupid now. Haven't checked it but sure it works.

avatar image DaveA · Mar 23, 2011 at 09:26 PM 0
Share

But wait, there's more! I would not trust print (total--) to do the right thing (although it might). Will it decrement the total THEN print it, or print it then decrement it? I think you can lose the entire Update function, and modify the other func to check for total==0 before doing the instantiate. Also make sure fireworks is assigned (presumably in the Inspector)

avatar image Jessy · Mar 23, 2011 at 09:28 PM 0
Share

It will decrement first.

avatar image Eric5h5 · Mar 23, 2011 at 10:00 PM 0
Share

@Jessy: no, that would be --total. total-- will print the total then decrement it.

avatar image DaveA · Mar 23, 2011 at 10:26 PM 0
Share

Which is why I usually opt for breaking it into separate statements: total--; print (total);

Show more comments
avatar image
1
Best Answer

Answer by Jessy · Mar 23, 2011 at 09:27 PM

Use ints. Floats are not really suitable for equality comparisons. Also, once you get that working, your current code is going to instantiate celebration continuously. Move that code to the Collision.

Comment
Add comment · Show 1 · 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 Muzz 1 · Mar 24, 2011 at 05:28 PM 0
Share

I was just about to ask that... I can't give this as correct too, but I'll vote it up.

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

On Button press Move Value toward 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Set variable/Array to a length by scripting 2 Answers

Get a variable from another file javascipt 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