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 /
This question was closed Oct 06, 2016 at 11:22 PM by bloxxyyy for the following reason:

The question is answered, right answer was accepted. Question would not be of importance to other people.

avatar image
0
Question by bloxxyyy · Oct 06, 2016 at 10:38 PM · c#listcounterspawning problemsdebug.log

list.count gives back odd numbers c#

I am using System.Collection.generic to count all the objects currently created, however at a inconsistent time mostly arround 7/12 objects created list.count gives back odd numbers like 27 or 35 as example. I think this has to do with the update method not being fast enough at loading the objects. But I am quite unsure.

PS. Also as you might have noticed, the code isn't very optimalized any tips are very welcome.

     using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class EnemySpawner : MonoBehaviour {

     private Material color;
     private int spawnedCreatures = 0;

     public int spawnCap = 5;
     public float spawnClosest = 3, spawnFarest = 15; // min and max spawn distance of each enemy

     private GameObject player;
     private List<GameObject> units = new List<GameObject>();
     private float playerPosX, playerPosZ;

     /*
      * set the material of the cube,
      * get the position of the player
     */
     void Start () {
         color = (Material)Resources.Load ("goblin");

         // we might eror on here if unity cant find the tag player but it should be fine because we set a tag in the player script
         player = GameObject.FindGameObjectWithTag ("Player");
         playerPosX = player.transform.position.x;
         playerPosZ = player.transform.position.z;
     }

     /*
      * loop to spawn enemies
      * get a new position for a enemy
      * check if the position of each enemy and the player is far away enough
      * Set a new game object, insert a cuber and set the material for the cube.
      * Set the position and add a tag Enemy
      * Last the script Enemy
      */ 
     void Update () {
         if (spawnedCreatures < spawnCap) {
             Vector3 newPosition = new Vector3 (Random.Range(playerPosX - spawnFarest, playerPosX + spawnFarest), 1f , Random.Range(playerPosZ - spawnFarest, playerPosZ + spawnFarest));
             GameObject emptyObject, cube;

             // Set all units in a list
             foreach (GameObject enemy in GameObject.FindGameObjectsWithTag ("Enemy")) {
                 units.Add (enemy);
             }
             units.Add (player);

             // check if the distance between all units is far away enough from echother.
             // else we restart the loop
             foreach (GameObject unit in units) {
                 if ((unit.transform.position - newPosition).magnitude < spawnClosest) {
                     return;
                 }
             }

             // set a new object
             emptyObject = new GameObject ("Goblin");
             cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

             cube.transform.parent = emptyObject.transform;
             cube.GetComponent<MeshRenderer>().material = color;
             emptyObject.transform.position = newPosition;
             emptyObject.tag = "Enemy";
             emptyObject.AddComponent<Enemy> ();

             spawnedCreatures = units.Count - 1; // -1 because we dont want to count the player
             Debug.Log(spawnedCreatures);
             units.Clear(); // clear the list of unallowed positions for the enemies. we dont want double entries
         }
     }
 }






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

  • Sort: 
avatar image
1
Best Answer

Answer by doublemax · Oct 06, 2016 at 11:07 PM

If you break out of Update() with the "return" in line 53 you don't clear the units list, so in the next Update() you add the same enemies to that list again.

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

Follow this Question

Answers Answers and Comments

247 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 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

Spawning a number of enemies while condition is true 0 Answers

Extract data from a litjson file and load it to a list C# 1 Answer

How Can I Download Multiple Files With Unity Web Request? 0 Answers

How to rotate an object 2 Answers

I need help finding the index of an object in a list. 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