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.