Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 fwdkhn345 · Dec 31, 2020 at 12:20 PM · enemyspawnhowdifferenttypes

How i can spawn enemies of different types???

I want to spawn 3 or 4 different type of enemies at same location? i have written a script which only spawns two type of enemies at same location i am new to unity and c# please helo me here is my spawn script... public GameObject enemy; public GameObject enemy1; public GameObject boss; public float speed = 5f; public bool ismoveright = true; public float min = -4f; public float max = 4f; public float padding = 3.5f; public float spawndelay = 3f;

 // Start is called before the first frame update
 void Start()
 {

    
         SpawnUntill();
        
   
 }

 //for adjusting enmies speed/movement
 void Update()
 {


     if (ismoveright == true)
     {
         transform.position += Vector3.right * speed * Time.deltaTime;
     }
     if (transform.position.x >= max)
     {
         ismoveright = false;

     }
     if (transform.position.x <= min)
     {
         ismoveright = true;

     }
     if (ismoveright == false)
     {
         transform.position += Vector3.left * speed * Time.deltaTime;
     }
     ScreenSizer();

     if (CheckDestroyedEnemy())
     {  
         SpawnUntill1();
         if (CheckDestroyedEnemy())
         {
             SpawnUntill2();
         }


     }
 }
 //for generating enemies one by one not together

 void SpawnUntill()
 {
     Transform position = ChildPosition();
     if (position)
     {
         GameObject Enemyprefab = Instantiate(enemy, position.transform.position, Quaternion.identity) as GameObject;
         Enemyprefab.transform.parent = position;
     }

     if (ChildPosition())
     {
         Invoke("SpawnUntill", spawndelay);
     }


 }
 void SpawnUntill1()
 {
     Transform position = ChildPosition();
     if (position)
     {
         GameObject Enemyprefab = Instantiate(enemy1, position.transform.position, Quaternion.identity) as GameObject;
         Enemyprefab.transform.parent = position;
     }

     if (ChildPosition())
     {
         Invoke("SpawnUntill1", spawndelay);
     }


 }
 void SpawnUntill2()
 {
     Transform position = ChildPosition();
     if (position)
     {
         GameObject Enemyprefab = Instantiate(boss, position.transform.position, Quaternion.identity) as GameObject;
         Enemyprefab.transform.parent = position;
     }

     if (ChildPosition())
     {
         Invoke("SpawnUntill2", spawndelay);
     }


 }
 
 //for moving enemies accordng to secreen size
 public void ScreenSizer()
 {

     float distancetoscreen = transform.position.z - Camera.main.transform.position.z;
     Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distancetoscreen));
     Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distancetoscreen));
     min = leftmost.x + padding;
     max = rightmost.x - padding;
 }
 //for checking all enemies are destroyed or not so we can produce new enemies
 bool CheckDestroyedEnemy()
 {
     foreach (Transform childobj in transform)
     {
         if (childobj.childCount > 0)
         {
             return false;

         }
     }
     return true;
 }
  
 //for generating enemies on positon in which it appers first time 
 Transform ChildPosition()
 {

     foreach (Transform childobj in transform)
     {
         if (childobj.childCount == 0)
         {
             return childobj;

         }
     }
     return null;
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How do I code transform translate in unity3d/ Not working right 0 Answers

My Code freezes Unity - not sure what to do... 1 Answer

Infinite instantiation w/ for loop? Please help! 1 Answer

2D How to get enemy to face the player while moving 0 Answers

Trying to create random encounters with percents 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