Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Initiating the Same Prefab Multiple Times - Ball Dropper

Discussion in 'Scripting' started by Zoneo, Aug 14, 2015.

  1. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    A loop?

    Code (csharp):
    1.  
    2. for(int i = 0; i < 5; ++i)
    3. {
    4.     Instantiate(prefab, new Vector3(0, i * 5, 0), Quaternion.identity);
    5. }
    6.  
    That will create them 5 units down the Y. Adjust the position as necessary.
     
  3. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Up at the top of your code, with your other variables, you'd have:
    Code (csharp):
    1.  
    2. public GameObject prefab;
    3.  
    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.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    tedthebug and GroZZleR like this.
  6. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Stop.

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

    This is the right answer. You just need to massage it a bit.
     
    MieskeB likes this.
  8. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    617
    Look really close at the vector 3 part.
     
  9. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
    Kiwasi likes this.
  12. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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?
    Code (csharp):
    1.  
    2.  
    3. public float timer;
    4.  
    5. void Start () {
    6.      timer = 1;
    7. }
    8. for(int i = 0; i < 5; ++I) {
    9.   Instantiate(prefab, (new Vector3(0, i * 5, 0), Quaternion.identity) * timer * Time.deltaTime);
    10. }
    11.  
    12.  
     
    Last edited: Aug 16, 2015
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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)
     
  14. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    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)'

    Code (csharp):
    1.  
    2. public GameObject prefab;
    3. public float timer;
    4. public float timerEnd;
    5.  
    6.  
    7.  
    8. void Start () {
    9. timer = 1;
    10. timerEnd = 5;
    11.  
    12.  
    13.  
    14. }
    15.  
    16. void Update (){
    17. if (timerEnd > 0)
    18. timerEnd -= Time.deltaTime;
    19. if (timerEnd < 0) {
    20. GameObject.SetActive (false);
    21. }
    22.  
    23.  
    24. if (timer >0) timer -= Time.deltaTime;
    25. if (timer < 0) {
    26. for (int i = 0; i < 1; ++i){
    27. Instantiate(prefab, new Vector3(0, i* 0, 0), Quaternion.identity);
    28. timer = 1;
    29.  
    30.  
    31. }
    32. }
    33. }
    34.  
     
  15. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
  16. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    617
    On line 27 change the y value to 0.
     
  17. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    @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?
     
  18. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    It creates an object at a given vector & given rotation in a game scene
     
  19. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    what is the for (int i = 0; i < 1; ++I)
     
  20. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
    Zoneo likes this.