Guest User

object pooling

a guest
Mar 9th, 2014
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* I'm making an object pool for bullets in my game. I had it working fine in JS, but I needed to convert this project  *  to C#. Now I receive the error: "Type 'object' does not contain a definition for 'gameObject' and no extension
  2. *  method 'gameObject' of type 'object' could be found"  
  3. *  It's referring to this line:
  4. *  newBullet.gameObject.transform.position = myTransform.position;
  5. *
  6. * This line:   var newBullet = gameManager.GetComponent<GameManager>().enemyBulletStack.Pop();
  7. * returns an object (a bullet). How do I return a game object?
  8. *
  9.  
  10.  public IEnumerator Shoot(float delay) // waits for 'delay' seconds, then shoots directly at the player
  11.     {
  12.         yield return new WaitForSeconds(delay);
  13.  
  14.         // get a bullet from the stack
  15.         var newBullet = gameManager.GetComponent<GameManager>().enemyBulletStack.Pop();
  16.  
  17.         // position and enable it
  18.         newBullet.gameObject.transform.position = myTransform.position;
  19.         newBullet.gameObject.active = true;
  20.  
  21.         // calculate the direction to the player
  22.         var shootVector = gameManager.GetComponent<GameManager>().player.transform.position - myTransform.position;
  23.  
  24.         // normalize this vector (make it length 1)
  25.         shootVector.Normalize();
  26.  
  27.         // scale it up to the correct speed
  28.         shootVector *= enemyBulletSpeed;
  29.         newBullet.motion = shootVector;
  30.     }
RAW Paste Data