- Home /
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);}
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613142521im_/https://answers.unity.com/themes/thub/images/avi.jpg)