Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by imgtah · Dec 08, 2014 at 02:34 PM · 2d gamerandomenemycreategenerate

Random enemy generator

Hello,

*Excuse my rusty English

I'm creating a 2D Game and trying to create a random moving enemy, and the code works fine in small numbers.. but when i enter more than 50 enemies the game become very heavy! and i want to create more than 50!! is there another solutions?

 using UnityEngine;
 using System.Collections;
 
 public class CreateRandomEnemy : MonoBehaviour {
     
     public GameObject enemyPrefab;
     public float numEnemies;
     public float xMin = 6F;
     public float xMax = 50F;
     public float yMin = 2F;
     public float yMax = -8F;
     
     void Start () {
         
         GameObject newParent = GameObject.Find("Scripts");
         
         for (int i = 0; i < numEnemies; i++)
         {
             Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
             GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
             octo.transform.parent = newParent.transform;
         }
         
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Dec 08, 2014 at 02:38 PM

Without seeing what might be on your enemy prefabs, including scripts, we can assuming creating 50+ gameobjects then is only heavy because of the instantiate call. This is where you may want to look into a pooling system that per-instantiates your game objects to be recycled and reused in your game when it loads.

Boon Cotter has a freebie that does just that. You can also look up Pool Manager in the asset store or around the net, there are plenty of tutorials on creating them.

I would start by implementing the game object pooling system and see if that helps. Otherwise you may want to also investigate the scripts on the enemy game objects to see if they are doing anything silly during Start or Update.

Comment
Add comment · 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 imgtah · Dec 08, 2014 at 05:35 PM 0
Share

Thank you, i will try it

avatar image imgtah · Dec 08, 2014 at 06:02 PM 0
Share

the script start generating but nothing appears in the screen!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy's initial attack doesn't deal damage to player 1 Answer

Move Enemy randomly in a range. 3D 0 Answers

How could i access to a specific collider box if it is in a children of the main Object? The script is in the main object. 1 Answer

Random enemy fire rate 4 Answers

Need help with my #c script to Generate 2 Obstacles 2 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