Search Unity

Object pool system on Brick Breaker game

Discussion in 'Scripting' started by MMT, May 15, 2013.

  1. MMT

    MMT

    Joined:
    Apr 12, 2013
    Posts:
    20
    Hey,
    I got this object pool system to work. The problem is that I really dont have any ideas how to position these objects I have loaded to memory. For example I have coins that should come from brick I hit with the ball. How do I get position from this brick to move my coin there?
     
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    you detect the "hit" from your ball with the brick, or? Then use this timing and instantiate the coin at ball.position. It's not that hard to figure a way out, as it's just Instantiate at position XY, but what you need is finding the right condition or time when you want to instantiate that coin. For example at the time when the ball hit's the brick, or when the ball is x meters away from something, or whatever, you're going to check for this event, so just use it also for your coins, that's it ;)
     
  3. MMT

    MMT

    Joined:
    Apr 12, 2013
    Posts:
    20
    okey thanks SlyRippe,

    so I dont want to Instantiate anything. the prefab (coin) is allready loaded in the scene but inactivated. So when I hit a brick with the ball one of the loaded coins becomes activated and appears to the screen. But I cant figure out how to get that position from the brick i just hit to tell the coin to start falling from that spot
     
  4. Tsumik1

    Tsumik1

    Joined:
    Oct 15, 2012
    Posts:
    49
    Presuming you are destroying your bricks when they are hit you could always use OnDestroy().
    So basically you have your brick which will contain a reference to a coin(Instantiating would have been easier for this), so every brick will need to have reference to a coin somewhere on screen.

    Then some code along these lines attached to the brick will do the trick.
    Code (csharp):
    1.  
    2.  
    3. void OnDestroy()
    4. {
    5.  //myCoin is the coin to be positioned by the brick.
    6.  //it would probably be worth putting an enabler script inside the instance of myCoin so that you can control rigidbodies etc.
    7.   myCoin.transform.position = transform.position;
    8.   myCoin.enabled = true;
    9. }
    10.