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 OWL7seven · Apr 11, 2014 at 10:50 PM · javascriptinstantiatepositioning

how to make multiplication code simpler.

i m trying to make the code simpler but not sure how. i want it to go to 99. is there anyway to do this without me typing it out?

basically what i m trying to do is generate 99(or any amount i desire to put in) of the gameobject (which spawns a random prefab) but i want this code below to place a the clones every 12 units from each other on the x axis.(like light posts on the road)

 // JavaScript
 var x = 12;
 
 var brick : Transform;
 function Start () {
    
         
             Instantiate(brick, Vector3 (x, -4, 0), Quaternion.identity);{
             Instantiate(brick, Vector3 (x * 2, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 3, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 4, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 5, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 6, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 7, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 8, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 9, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 10, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 11, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 12, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 13, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 14, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 15, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 16, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 17, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 18, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 19, -4, 0), Quaternion.identity);}{
             Instantiate(brick, Vector3 (x * 20, -4, 0), Quaternion.identity);}
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Apr 11, 2014 at 10:52 PM

You're looking for a loop, which allows you to run the same (or similar) code repeatedly according to some condition.

Example:

 for (var i=1; i<100; i++) {
     Instantiate(brick, Vector3 (x*i, -4, 0), Quaternion.identity);
 }

The above code will execute 99 times, with variable i ranging from 1 to 99. For more information, you can Google "javascript for loop example" and look around until things make sense to you.

Basic loop types include while loops (which usually execute until some condition is met) and for loops (which usually execute some number of times).

Loops are a very powerful tool. Just be careful that you don't write an infinite loop which never ends!

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 OWL7seven · Apr 11, 2014 at 10:54 PM 0
Share

thanks alot

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Space Shooter Instantiating bullet question 1 Answer

Referencing instantiated player in camera's Start() 2 Answers

Trying WaitForSeconds before Instantiating 2 Answers

How to Instantiate prefab as child? (Java) 1 Answer

Prevent instantiating on collider 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