Search Unity

Instantiating with a List

Discussion in 'Scripting' started by LuckyStreak, Apr 1, 2015.

  1. LuckyStreak

    LuckyStreak

    Joined:
    Apr 1, 2015
    Posts:
    9
    Hello,

    I am trying to keep a counter of gameObjects, then run a loop and destroy those gameObjects when cubicideCount == 1. My code works, but only sometimes. I noticed a couple things..

    1. The enemyCounter.Count hits a limit of 30 then stays there.

    2. The enemyCounter.Count will count properly for a while, then get all sporadic switching between 1 and 0.

    Please note that cubicideCount is incremented somewhere else in a OnCollisionEnter().

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class CreateCubes : MonoBehaviour {
    8.  
    9. void Start () {
    10.         //==Loop the Create() which creates an enemy every second.==//
    11.         InvokeRepeating ("Create", 1, 1);
    12.           }
    13.  
    14. void Update(){
    15.         //==Check if enemyCounter is equal to 1, and run a for loop to destroy every gameObject in the list.==//
    16.         //==Then==//
    17.  
    18.         if(GameObject.Find("Player(Clone)").GetComponent<CollideAndExpand>().cubicideCount == 1){
    19.             for(int i = 0; i < enemyCounter.Count; i++)
    20.                 Destroy (enemyCounter[i].gameObject);
    21.                 enemyCounter.Clear();
    22.                 GameObject.Find("Player(Clone)").GetComponent<CollideAndExpand>().cubicideCount = 0;
    23.         }
    24.  
    25.        
    26.         }
    27.  
    28. //======Create the enemy and assign it an element in a list======/
    29. void Create () {
    30.         enemyCounter.Add((GameObject) Instantiate (enemyCubes, randomBoxLocation, transform.rotation));
    31. }
    32.  
    33. }
    34.  
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'm guessing this is partial code, because enemyCounter, enemyCubes and randomBoxLocation arn't declared anywhere.

    First big thing I see is that you should be caching the CollideAndExpand component, instead of looking for it twice every frame. That's an expensive operation.

    Like so:
    Code (CSharp):
    1.     CollideAndExpand collideAndExpand;
    2.  
    3.     void Start () {
    4.         //This assumes that your player object has a player tag, has a CollideAndExpand component, and that the
    5.         //player object is created before this script executes. If it doesn't, you could always check collideAndExpand
    6.         //for null in the update loop and find it there. That way it only looks for it once.
    7.         collideAndExpand = GameObject.FindGameObjectWithTag ("Player").GetComponent<CollideAndExpand> ();
    8.     }
    Anyway, superceding that, I'm betting the problem is originating from another part of your code.
    Although I would have written this differently, this will behave as expected.