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 qtoompuu · Oct 25, 2016 at 04:52 AM · listserializationspawninglists

List Data not persisting after being added in Start()

Hello all, I appologize, as this is likely a dumb question, but I am not used to using generic lists. I am trying to make a script that will control all of the enemy spawners in my scene. I need to hold the spawners in a genaric list, so that I can randomly select several of them(using the shuffle technique) and spawn enemies at them. the problem is that after finding and adding all the spawners and upon exiting the Start() function the list erases it self and is empty. I have read in several places that I need to serialize the list, but None have explained how to do this. My code is as follows

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

 public class VRZ_SpawnController : MonoBehaviour {
 
     private List<GameObject> SpawnList;
     private int wave;
     private int spawners;
     private int totalEnemies;
     private int liveEnemies;
     private int groupSize;

     public float initialStartDelay;
     public int waves;
     public int maxSpawnsPerWave;
     public int maxSpawns;
     public int maxGroupSize;
     public float maxSpawnDelay;
     public bool allowPrioritySpawn;
     // Use this for initialization
     void Start () {
         spawners = transform.childCount;
         List<GameObject> SpawnList = new List<GameObject>();
         print(SpawnList.Count + "@1");
         for (int i = 0; i < spawners; i++) //finds all the child spawners and adds them to the list, this part works fine
         {
             SpawnList.Add(transform.FindChild("Spawn (" + i + ")").gameObject);
         }
         print(SpawnList.Count + "@2");
         StartCoroutine("Controller", initialStartDelay); //begins the spawning coroutine

     }
     int ZCount() // This counts total enemies alive, and is also working fine
     {
         print(SpawnList.Count + "@4");
         int go = 0;
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag("Enemy");
         go = gos.Length;
         liveEnemies = go;
         return go;
     }
     private IEnumerator Controller()
     {
         while(true)
         {
             print(SpawnList.Count + "@3");
             ZCount();
             if (liveEnemies < maxSpawns && totalEnemies < maxSpawnsPerWave) // checks is too many enemies are spawned
             {
                 print(SpawnList.Count + "@5");
                 int activeSpawners = Random.Range(0, SpawnList.Count);
                 List<GameObject> Temp = new List<GameObject>();
                 Temp = SpawnList;
                 for (int i = 0; i < activeSpawners; i++)
                 {
                     groupSize = Random.Range(1, maxGroupSize);
                     int pos = Random.Range(0, Temp.Count);
                     Temp[pos].GetComponent<VRZ_Spawn>().Spawn(groupSize);
                     Temp.RemoveAt(pos);
                 }
             }
             yield return new WaitForSeconds(10);
         }
     }
 }

Only the print(SpawnList.Count + "@2"); shows that there is anything in the list, but all print statements are reached. I just can't seem to figure out why the list clears itself after the start function

Thanks in advance for the help!

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
Best Answer

Answer by qtoompuu · Oct 25, 2016 at 05:26 AM

Ok I solved it, for whatever reason adding the spawns using a function called from within start rather than directly doin it in start worked...

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 Ahndrakhul · Oct 25, 2016 at 05:44 AM 0
Share

I'm guessing that your issue was caused by scope and name hiding. You declared the list once in your class and again with the same name in the start() method. if you changed

 List<GameObject> SpawnList = new List<GameObject>();

to

 SpawnList = new List<GameObject>();

in the start() method, it would probably work.

I think that you were creating a new "SpawnList" variable that was local to the start() method. You filled that local list in start(), so your print statement worked there, but as soon as the start method finished, that list went out of scope and was lost. The class-scoped List was then used in the rest of your methods, and it was never filled with items. I'm surprised you didn't get null reference errors.

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

79 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

Related Questions

How do I modify variables from a different class that belongs to the same C# file? 1 Answer

Find the closest Enemy that has a tag added in a list? 1 Answer

Problem with Lists and Remove 0 Answers

How do I save 2 different string lists? 1 Answer

Why is my list bigger than it should be? 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