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 /
avatar image
0
Question by hallonkatastrof · Dec 06, 2016 at 05:36 AM · c#spawninglimitenemies

C# - spawn limit not working properly.

I'm trying to put a limit to how many "objects" can spawn, using the amount specified as "maxEnemies". But nothing whatsoever spawns, and I don't know why.

 using UnityEngine;
 using System.Collections;
 
 public class enemyAI : MonoBehaviour {
 
 
     private Vector2 spawnPosition;
     public GameObject[] objects;
     public float timer = 5f;
     public int maxEnemies = 10;
 
     // Use this for initialization
     void Start () 
     {
 
     }
 
     void Spawn() 
     {
         spawnPosition.x = Random.Range (-6.44f, 6.12f);
         spawnPosition.y = Random.Range (7.76f, 7.57f);
         Instantiate (objects[UnityEngine.Random.Range(0, objects.Length)], spawnPosition, Quaternion.identity);
     }
     
     // Update is called once per frame
     void Update ()
     {
         timer -= Time.deltaTime;
         if (timer <= 0f && objects.Length < maxEnemies) {
             Spawn ();
             timer = 2f;
         } 
         objects = GameObject.FindGameObjectsWithTag ("Enemy");
     }
     
 
 }
Comment
Add comment · Show 2
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 roman_sedition · Dec 06, 2016 at 05:47 AM 0
Share

Put a debug log in your spawn function, is it reaching there? If not maybe check if your enemies have the right tag.

avatar image JedBeryll · Dec 06, 2016 at 06:20 AM 0
Share

maxEnemies is public so it shows up in the inspector. Are you sure the value is not 0 in the inspector?

1 Reply

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

Answer by Hanoble · Dec 06, 2016 at 07:34 AM

I am fairly certain you have an issue with your objects[]. What you are trying to do makes little sense, and I think you probably have a logic issue here. At first, it appears that your objects[] is holding some prefabs for objects you want to randomly spawn, because of this line here:

  Instantiate (objects[UnityEngine.Random.Range(0, objects.Length)], spawnPosition, Quaternion.identity);

That line means that you must have some objects drug into the objects array in the inspector, or else you could not possibly instantiate anything, as it would null reference and spawn nothing (this is most likely your current problem). However, you then tell that same array to equal the GameObjects you find with the tag "Enemy", which at that point could ever only possibly be the one you just spawned. This means the next time Spawn() ran you would just be spawning the exact same enemy over and over.

I think you want to have an objectsToSpawn[] for the random selection used in the Spawn() (that way you would randomly spawn one of those objects that you drug into the array via the inspector), and then the objects[] array is those objects you have spawned and limit the number of spawns with the objects.Length check.

The entire thing has some stink to it, so I would revisit the logic and make sure you understand what your code is doing. If need be, there are a lot of other answers out there for this same logic.

Here are a few of them for you. It actually looks like you are trying to take parts from a couple of these and put them together, but I would advise that you take some time and make sure you truly understand what your code is doing- it will pay off majorly in the long run!

http://answers.unity3d.com/questions/810806/spawn-random-enemy.html https://unity3d.com/learn/tutorials/projects/survival-shooter/more-enemies https://www.youtube.com/watch?v=-s6jobwtuRk

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 hallonkatastrof · Dec 06, 2016 at 08:10 PM 0
Share

The problems are not quite the same as you described, but it's probably wise to go back and make sure I understand. Thanks!

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

11 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

Related Questions

Enemies spawn on top of each other(C#)(Unity) 1 Answer

Unity2D spawn frequency not working C# 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[C#] How to spawn multiple objects that share the same base-class 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