Initiating the Same Prefab Multiple Times - Ball Dropper

Hey guys,

I’m trying to make a script where a ball prefab is created multiple times in order to drop a bunch of balls into a funnel. whats the best way to go about it?

A loop?

for(int i = 0; i < 5; ++i)
{
    Instantiate(prefab, new Vector3(0, i * 5, 0), Quaternion.identity);
}

That will create them 5 units down the Y. Adjust the position as necessary.

I haven’t really learned loops yet, how do I declare the prefab variable? I basically just want a bunch of balls to be dropped at the start of the game

Up at the top of your code, with your other variables, you’d have:

public GameObject prefab;

Then in the editor, you drag your prefab object onto that variable to assign it to your ball prefab. Have you done the Roll A Ball tutorial here on the site? It will teach you all about things like this.

Loops are kind of fundamental to programming. Variables, branching and loops are considered the fundamentals that every language must implement. Without these you won’t get far.

Go onto the learn section and do the beginner scripting tutorials. There are good videos there for all of the basic C# principles.

2 Likes

I’m not sure this is the code I’m looking for… This seems to place them 5 units apart from each other, which isn’t what I want. what I want is for it to create the same prefab in the same place multiple times so that all of the balls it creates will fall into a funnel type thing for my game. Not create a row of balls.

Stop.

Read the code. Learn how loops work. Then try again.

This is the right answer. You just need to massage it a bit.

1 Like

Look really close at the vector 3 part.

I really don’t think this is right. After playing with the vectors my original though becomes more and more believable to me. this script would be great if I were looking to build something, like a wall. That is not what I am trying to do. When I set the coordinates to (0,0,0) to create them all in the same place (like I need it to do) except instead of them falling to the floor, they shoot out in either direction on the X axis (I assume because of the rigidbody being duplicated on itself like that) I cant remove the rigidbody because then it wont interact with my level properly.

You’ve got your logic messed up. Multiple physics objects can’t occupy the same space at the same time. That craziness you see is the engine trying to compensate.

What do you actually want to achieve? Think it through logically first, then code will follow.

If you want to instantiate them then implement a timer between each instantiation. If all you want to do is have multiple balls fall down once & don’t care about instantiatiom then just place multiple balls at varying heights in the scene. When you run the scene they will all fall & hit the ground at different times due to the different heights you originally had them at.

1 Like

what I want to achieve is a “Ball Dropper.” my level looks like this: a funnel at the top and a basket at the bottom. What I need is simple: an empty game object with the script “ballDropper” attached. What the script needs to do, is create multiple balls that will simply fall into my funnel, directing them into the basket. it needs to basically act like a spout. A timer would probably solve this, cause if it creates them with like a second apart then the one already created would have fallen out of the way.
Would this do it?

public float timer;

void Start () {
     timer = 1;
}
for(int i = 0; i < 5; ++I) { 
  Instantiate(prefab, (new Vector3(0, i * 5, 0), Quaternion.identity) * timer * Time.deltaTime);
}

I’d have the timer then in update use
if(timer>0) timer-=time.deltatime;
If(timer<=0{
instantiate…
timer=1; // or whatever you want the timer to be. It will need to be large enough to let the ball drop so it won’t be touching the next one that instantiates.
}

Something like this will just instantiate a continuous stream of them. Make sure you destroy them at some point otherwise you end up with a memory leak.

(Sorry, couldn’t get tags to work & I don’t have access to a PC to check the code. I know there’s bits wrong but it should be a start)

the timer works, and it drops without interference. Now I’m trying to set up a timer to deactivate the ball dropper after a set time. Also, it seems to be creating the balls in the wrong spot on the map. Ive played around with the vector3 values but it doesn’t seem to have any relation to where its made, even though its the position parameter.
Assets/_Scripts/Scene 02/ballDropper.cs(23,36): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.SetActive(bool)’

public GameObject prefab;
public float timer;
public float timerEnd;



void Start () {
timer = 1;
timerEnd = 5;



}

void Update (){
if (timerEnd > 0)
timerEnd -= Time.deltaTime;
if (timerEnd < 0) {
GameObject.SetActive (false);
}


if (timer >0) timer -= Time.deltaTime;
if (timer < 0) {
for (int i = 0; i < 1; ++i){
Instantiate(prefab, new Vector3(0, i* 0, 0), Quaternion.identity);
timer = 1;


}
}
}

Your instantiate seems to be using a Y value of i*0 which will always be 0. If you have an empty game object at the position you want the balls to appear then make it a public game object on the ball dropper script, drag it onto the script in the inspector, then in your instantiate use spawner.transform.position, spawner.transform.rotation as the location & rotation. If they are still behaving weirdly increase the timer to 5, if they start behaving correctly then gradually drop the timer as the issue may be caused by them spawning in each other & reacting.

On line 27 change the y value to 0.

@tedthebug that did it! thanks! one last question about this though… I don’t think I really understand what exactly the Instantiate function does, and reading the documentation on it hasn’t helped. could you explain it in your own words for me?

It creates an object at a given vector & given rotation in a game scene

what is the for (int i = 0; i < 1; ++I)

It’s a loop. It’s saying to start from 0 & every time it is less than 1 do something then increment the counter by 1.
In this case it starts at 0, which is <1 so it will do whatever you say, then it will add 1 then start again. The next # is 1, which is not < 1, so it will exit the loop.

1 Like