Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Jedd117 · Mar 18, 2016 at 12:40 AM · 2dspawnprefabs

2D: Spawning prefabs at random points above the screen?

I have a game where asteroids are spawning from the top just out of camera view falling down and you have to avoid them. I'm having trouble getting them to spawn. From reading and watching videos, It looks like that I'm supposed to make a prefab for the asteroid, then create an empty GameController. Within that GameController, I've created an empty GameObject and dragged my asteriod prefab into that spot. In the script, I reference the name of the prefab but it doesn't spawn. I know it's probably something simple that I'm just overlooking, or I'm not approaching this from the right way. (I wouldn't be surprised because this is my first time messing with Unity.)

Here's my script:

public class GameController : MonoBehaviour { public GameObject hazard; // Asteroid is placed in this on the inspector

void start() { SpawnWaves(); }

void SpawnWaves() { Vector3 spawnPosition = new Vector3(); // creates a new position to spawn object

     Quaternion spawnRotation = new Quaternion(); // quaternion is spawn rotation, check for 2d objects.

     Instantiate(hazard, spawnPosition, spawnRotation);  // used to spawn the asteroid also known as hazard. 

 }

}

Comment
Add comment · Show 1
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 Bonfi_96 · Mar 18, 2016 at 01:12 AM 1
Share

Try Start() ins$$anonymous$$d of start()

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jedd117 · Mar 18, 2016 at 03:37 AM

Thanks for the response! I was reading more after I posted this and I changed my code some. Here it is if any new people need something to start with. I've added comments to explain some of the code.

Script (c#):

public class GameController : MonoBehaviour {

public GameObject hazard;

 void Start()
 {
     SpawnWaves();
 }
 void SpawnWaves()
 {
     Vector3 position = new Vector3(Random.Range(-8.5f, 8.5f), 8, 0);  // creates a new position to spawn object created this by getting the "x" and "y" positions.

     Instantiate(hazard, position, Quaternion.identity);  // hazard is prefab, position declared variable above. Quanternion is the spin. By addeding "identity", it's set to none.
 }

}

Comment
Add comment · Show 1 · 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 kskjadav007 · Aug 24, 2017 at 09:15 AM 0
Share

thank you help me a lot

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Random array with prefabs? 2 Answers

Spawn different player prefab unet 0 Answers

Wrong position on spawn via script on every object (2.973684 instead of 3) 0 Answers

Prefab Powerup works, but instantiated clone does not 1 Answer

The spacing between sprites are inconsistent 0 Answers


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