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 QuesoMan32 · Dec 12, 2017 at 03:13 PM · spawningspawning problems

Putting a cap on enemies spawned

Trying to set a cap on the amount of enemies spawned by having a public variable called 'enemiesSpawned' shared between two scripts. Not only do I not know how to script it to keep the cap at, say, 25, but getting the component between the two scripts simply isn't working. (C#, trying to use gameObject.GetComponent<>().variable but not working). The first script is my enemy spawning script, and the second is the script that contains the die method, of which I'm planning on decreasing the variable enemiesSpawned by 1 each time it is executed to keep the number of enemies spawned at or below 25.

 using UnityEngine;
 
 public class EnemySpawn : MonoBehaviour
 {
     
     public GameObject enemy;                
     public float spawnTime = 3f;            
     public Transform[] spawnPoints;
     
 
     
         void Start()
         {
             
         
             InvokeRepeating("Spawn", spawnTime, spawnTime);
         
            
         }
 
 
         void Spawn()
         {
 
 
             
             int spawnPointIndex = Random.Range(0, spawnPoints.Length);
 
           
             Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 
         
         
 
         }
      
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Target : MonoBehaviour
 {
 
     public float TargetHealth = 50f;
     public float enemiesSpawned;
     
 
 
 
    
     
     public void TakeDamage(float amount)
     {
         TargetHealth -= amount;
         if (TargetHealth <= 0f)
         {
             Die();
             
         }
     }
 
     public void Die()
     {
         Destroy(gameObject);
         enemiesSpawned--;
     }
 
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheAltbacker · Dec 12, 2017 at 03:57 PM

I'd put the enemiesSpawned variable in the EnemySpawn class as, if I understand well, Target has multiple instances, so there would be multiple different values for each enemy. You should be able to get a reference to the 'EnemySpawn' class using:

 public EnemySpawn enemySpawn;
         //you can then drag and drop the relevant GameObject onto this script to get the reference
         //
         //
         // then in void Die()
 enemySpawn.enemiesSpawned -= 1;

It is also important to increase enemiesSpawned when you are spawning them.

As for capping the spawns, I believe having the Spawn() method check for the number of enemies before spawning is the easiest.

  void Spawn()
  {           
       if (enemiesSpawned <= 24)
       {
                  int spawnPointIndex = Random.Range(0, spawnPoints.Length);       
                
                  Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation); 
     
       }
 }

This should solve your problem, although I haven't tested it. Hope it helps anyways.

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 Full8uster · Dec 12, 2017 at 05:01 PM

Try create these vars on your EnemySpawn script. Set enemy as your enemy gameobject.

 public Target targetScript;
 public GameObject enemy;

On Start:

 targetScript = enemy.GetComponent<Target>() as Target;

Then to define a cap to the enemies that will be Instantiate create a var on EnemySpawn and add++ to each enemy instanted:

 public var enemiesSpawned;

On the Spawn method:

 void Spawn()
          {
             
              if (enemiesSpawned <= 25){
                 int spawnPointIndex = Random.Range(0, spawnPoints.Length);
  
            
                Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

                enemiesSpawned++;
             }
       
          }

Then to acess your other script is just yse the targetScript var to get and set values from it.

I don't know if it is exactly what you want, but hope helped somehow.

If you need me, I'm at your disposal!

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

73 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

Related Questions

problem when i spawn to many Enemy the game be slower 1 Answer

player spawning somewhere distance in the field? 0 Answers

Instantiate along the Z/X Axis only? 0 Answers

How to spawn multiple prefabs at different spawn rates 0 Answers

Make a gameobject spawn before enemies spawn? (C#) 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