- Home /
Spawn a power up after asteroid death
So I am making an asteroids clone to get used to Unity. I have the Asteroids spawning and such and destroying via my bullets from the ship and a GUI that updates the score/lives as well as a shield power up(that has 2 uses per pickup).
I made a PowerUpSpawner to hold the shield and any future powerups I make.
public class PowerUpManagerBehaviour : MonoBehaviour
{
public List<GameObject> PowerUps = new List<GameObject>();
public void SpawnPowerUp(Vector3 position)
{ var powerUp = Random.Range(0,PowerUps.Count); var randomAngle = Random.Range(0,360); PU = PowerUps[powerUp]; Instantiate(PU,position, Quaternion.AngleAxis(randomAngle,Vector3.up)); } }
Now what I want to do is call the SpawnPowerUp and get a GameObject back in my AsteroidsBehaviour like this.
void OnTriggerEnter(Collider colliderObject)
{
if(colliderObject.transform.tag.Equals("Bullet"))
{
Destroy(gameObject);
Instantiate(AsteroidExplosion,transform.position,transform.rotation);
//Access powerupspawner and spawn a power up.
var PUMB = GameObject.Find("PowerUpSpawner");
PUMB.GetComponent().SpawnPowerUp(transform.position);
}
}
}
Now even though it seems like it should spawn the powerUp (a random one via the Spawnpowerup) when the asteroid dies, it doesn't do anything. I have my shield two times in my PowerUpSpawner GameObject in the scene so I'm not sure why it's not spawning at all...
Any help would be really appreciated.
EDIT: Cleaned up the code for the non important and empty methods etc.
Well we're really only using Unity in school for prototyping. Asteroids is just to get used to how the engine and scripting works.
yea cause right now I do destroy(gameObject) on the Asteroid and then instantiate the explosion particle system and then instantiate the powerup (which if it's anything else it seems to work cept for my shield...) I'll give the Invoke a shot. Thanks for the suggestion.
2nd/3rd. Finishing up my 2nd year courses and doing 2 from my third year as well.
Invoke didn't work, can't get it to work at all...$$anonymous$$an this is really bugging me that the shield won't spawn but other stuff will...
I can't see anything obviously wrong in your code. In these types of situations I'll often isolate the code that should be working (in this case your powerup spawning code) and attach it to a button press or something to see if it works as I think it should be working. It's kind of a binary search problem - if the powerup spawning code works by itself then you know there's probably something wrong with what's executing the code - if it doesn't work by itself then you know it's something wrong with the spawner. You can then start breaking those pieces down into smaller pieces, until eventually you'll find the offending thing.
Answer by MD_Reptile · Oct 07, 2012 at 05:34 PM
I personally made an asteroids style game, called asteroid buster, but anyway how i handled power ups was to generate a random number and then have certain ranges select a certain power up. Like that way i could have certain ones more likely than others to get spawned. So when you do on Collision enter, generate a number, then spawn a prefab for your powerup based on that number, then destroy the asteroid afterwards. Hope this helps!
That's exactly what I'm trying to do as well. I have no idea what the bug in my code is but I just added Lazers to test...destroy the asteroids, shield shows up...I haven't changed a thing.
if you just edit your code, drastically taking out empty functions, irrelevant calls etc, someone might be able to see the woe!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to spawn gameobject from host to all clients 1 Answer
Spawn OnTriggerEnter 0 Answers
Instantiate giving a NullReferenceError 1 Answer
Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer