• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by TooL · Oct 07, 2012 at 11:14 AM · c#instantiatespawn

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.

Comment

People who like this

0 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TooL · Oct 07, 2012 at 02:30 PM 0
Share

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.

avatar image TooL · Oct 07, 2012 at 03:08 PM 0
Share

2nd/3rd. Finishing up my 2nd year courses and doing 2 from my third year as well.

avatar image TooL · Oct 07, 2012 at 03:56 PM 0
Share

Invoke didn't work, can't get it to work at all...Man this is really bugging me that the shield won't spawn but other stuff will...

avatar image Dave-Carlile · Oct 07, 2012 at 05:10 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image

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!

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TooL · Oct 07, 2012 at 05:37 PM 0
Share

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.

avatar image Fattie · Oct 07, 2012 at 06:07 PM 0
Share

if you just edit your code, drastically taking out empty functions, irrelevant calls etc, someone might be able to see the woe!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Respawning Single Enemy at a Random Range 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges