Search Unity

C# Weird array problem

Discussion in 'Scripting' started by Jordan104, Feb 12, 2016.

  1. Jordan104

    Jordan104

    Joined:
    Jun 4, 2013
    Posts:
    20
    I'm having this really weird and frustrating issue and I have no idea why. I have a pool of each particle effect needed for my gunscript to cut down on the amount of instancing that goes on at runtime. For some reason, the first array gets filled with objects meant for a completely different array and leaves the others empty. This is especially strange since I use very similar code for my AI script and it works fine.



    Here's an example of the code that plays up
    Code (CSharp):
    1.    
    2.     // Particles
    3.     public GameObject genericHit;
    4.     public GameObject metalHit;
    5.     public GameObject bloodHit;
    6.  
    7.     // pooling stuff
    8.     public GameObject[] genericPool;
    9.     public GameObject[] metalPool;
    10.     public GameObject[] bloodPool;
    11.  
    12.     void Start () {
    13.         gm = GameObject.Find("GameManager").GetComponent<GameManager>();
    14.         wm = this.GetComponentInParent<WeaponManager>();
    15.         asource = this.GetComponentInParent<AudioSource>();
    16.  
    17.         // pooling stuff
    18.         if (this.name == "Pistol") ammoPool = wm.pistolPool;
    19.         if (this.name == "Rifle") ammoPool = wm.riflePool;
    20.         if (this.name == "Shotgun") ammoPool = wm.shotgunPool;
    21.         if (this.name == "MG") ammoPool = wm.mgPool;
    22.         if (this.name == "Special") ammoPool = wm.specialPool;
    23.         if (this.name == "Rocket Launcher") ammoPool = wm.rockets;
    24.         if (this.name == "Grenade Launcher") ammoPool = wm.grenades;
    25.         PoolingParent = GameObject.Find("GameManager").transform;
    26.         StartPooling();
    27.         // end pooling stuff
    28.     }
    29.    
    30.     // this is the function that plays up
    31.     void StartPooling ()
    32.     {
    33.         // Particle effects
    34.         genericPool = new GameObject[magCapacity];
    35.         for (int a = 0; a < genericPool.Length; a++)
    36.         {
    37.             GameObject gp = Instantiate(genericHit);
    38.             gp.transform.parent = PoolingParent;
    39.             gp.SetActive(false);
    40.             genericPool[a] = gp;
    41.         }
    42.         metalPool = new GameObject[magCapacity];
    43.         for (int b = 0; b < metalPool.Length; b++)
    44.         {
    45.             GameObject mp = Instantiate(metalHit);
    46.             mp.transform.parent = PoolingParent;
    47.             mp.SetActive(false);
    48.             genericPool[b] = mp;
    49.         }
    50.         bloodPool = new GameObject[magCapacity];
    51.         for (int c = 0; c < bloodPool.Length; c++)
    52.         {
    53.             GameObject bp = Instantiate(bloodHit);
    54.             bp.transform.parent = PoolingParent;
    55.             bp.SetActive(false);
    56.             genericPool[c] = bp;
    57.         }
    58.     }
    A screenshot of the inspector to better illustrate the problem.



    The following code sample is from my AI script and works perfectly fine, as you can see it's almost identical to the code for before.

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         life = GetComponent<Health>();
    4.         Current_Ammo = Magazine_Capacity;
    5.         if (faction == Faction.Friend) gameObject.tag = "Friendly";
    6.         if (faction == Faction.Enemy) gameObject.tag = "Enemy";
    7.         Anim = GetComponent<Animator>();
    8.         Nav = GetComponent<NavMeshAgent>();
    9.         Limbs = GetComponentsInChildren<Rigidbody>();
    10.         Audio = GetComponent<AudioSource>();
    11.         for (int i = 0; i < Limbs.Length; i++)
    12.         {
    13.             Limbs[i].isKinematic = true;
    14.             Limbs[i].transform.tag = "Blood";
    15.         }
    16.         Weapon.transform.tag = "Metal";
    17.         PoolingParent = GameObject.Find("GameManager").transform;
    18.         StartPooling();
    19.     }
    20.    
    21.     void StartPooling ()
    22.     {
    23.         BulletPool = new GameObject[Magazine_Capacity];
    24.         for (int a = 0; a < BulletPool.Length; a++)
    25.         {
    26.             GameObject bu = Instantiate(Bullet);
    27.             bu.transform.parent = PoolingParent;
    28.             bu.SetActive(false);
    29.             BulletPool[a] = bu;
    30.         }
    31.         CasePool = new GameObject[Magazine_Capacity * 2];
    32.         for (int b = 0; b < CasePool.Length; b++)
    33.         {
    34.             GameObject ca = Instantiate(Case);
    35.             ca.transform.parent = PoolingParent;
    36.             ca.SetActive(false);
    37.             CasePool[b] = ca;
    38.         }
    39.         FlashPool = new GameObject[Magazine_Capacity / 3];
    40.         for (int c = 0; c < FlashPool.Length; c++)
    41.         {
    42.             GameObject fl = Instantiate(Flash);
    43.             fl.transform.parent = Muzzle.transform;
    44.             fl.SetActive(false);
    45.             FlashPool[c] = fl;
    46.         }
    47.     }
    I really have no idea what is causing the problem.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    look in line 48 and 56 ypou addding to wrong arrays
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    should be metalPool

    should be bloodPool

    copy paste mistake ;)

    therefore your generic pool hast bloodhits referenced
     
  4. Jordan104

    Jordan104

    Joined:
    Jun 4, 2013
    Posts:
    20
    This has to be the dumbest thing I've done in a long time, I've literally been puzzling at this for hours. Thanks for the help.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Don't worry, you never really stop making these errors. I'm not exaggerating either. You only (hopefully) get faster at spotting them.

    You might find it useful to invite your dog over and explain it to him, also known as "The Woof Method." See my avatar picture for details.

    "So here it is Rover, I instantiate the MetalHit, I set its parent, I set it to inactive, then I add it to the ... er, genericPool... okay, thanks Rover, here's a doggie treat... that's a good dog."