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 alex.rundquist · Apr 06, 2016 at 10:47 AM · spawn

how to make a enemy spawn in a specific place? 2d c#

Hello guys and galls I am a beginner and I wonder how make a enemy spawn in a specific place. Right now i only have one enemy in a place moving towards me and when i hit it the game crashes because the code is searching for an enemy that does not exist, so i want it to spawn every few seconds. please help me :(

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 Fredex8 · Apr 07, 2016 at 01:29 PM 1
Share

This tutorial explains it pretty simply. Or just copy the code from it and work it out. InvokeRepeating isn't the best method to use but it is a good starting point before developing anything more complex.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zoogyburger · Apr 09, 2016 at 10:21 PM

This is my enemy spawner script

 {
     public float spawnTime = 5f;
     //The amount of time between each spawn.
     public float spawnDelay = 3f;
     //The amount of time before spawning starts.
     public GameObject[] enemies;
     //Array of enemy prefabs.
     public Vector3 enposition;
     void Start ()
     {
         //Start calling the Spawn function repeatedly after a delay.
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     void Spawn ()
     {
         //Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[enemyIndex], enposition, transform.rotation);
     }
 }


Comment
Add comment · 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
0

Answer by ChaosEaterY · Oct 22, 2021 at 09:34 AM

{ public float SpawnDelay = 3f; public GameObject Enemy; public Vector3 SpawnPoint;

  float timeSinceLastSpawn = Mathf.Infinity;

  void Update()
  {
       timeSinceLastSpawn += Time.deltaTime
       if (timeSinceLastSpawn >= SpawnDelay)
       {
            Instantiate(Enemy, SpawnPoint, Quaternion.identity);
            timeSinceLastSpawn = 0;
       }
  }

}

Comment
Add comment · 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
0

Answer by EvilBob99 · Oct 26, 2021 at 09:15 AM

ummm I think you would want your enemy to be a prefab you can spawn and just create an empty and create a new enemy from there every so often be cause based on your code that seems like what you want to do so find a way to set up a timer like maybe 5 seconds and have it go down when it reaches 0 a new enemy is spawned at the empty and it resets I know I haven't helped very much but I'm trying to give you an idea of how to do this

Comment
Add comment · 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

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

58 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

Related Questions

Spawning Bug. 1 Answer

NetworkServer.SpawnWithClientAuthority not working? 1 Answer

How to drop a box 0 Answers

error CS1526: A new expression requires () or [] after type 1 Answer

[uNET] Network objects spawned not spawn in new clients 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