Search Unity

Create variables using gameObject.name

Discussion in 'Scripting' started by DanielRiches, Mar 25, 2022.

  1. DanielRiches

    DanielRiches

    Joined:
    Nov 23, 2019
    Posts:
    166
    i'm trying to set my game up so it will automatically set up a pool for some weapon objects, then when my character picks up the gun, it will attempt to grab the items referenced on the guns from the appropriate pools when the gun is fired.

    currently i'm using Resources.LoadAll to set up an array of Muzzle Flashes, Bullet Impacts and Bullet Tracers, this all works fine.

    Now I need to create a pool for each object in those arrays, and I would like if possible for each pool variable name to be the gameObject.name

    Heres the code so far:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Pool;
    5.  
    6. public class BulletTracerPool : MonoBehaviour
    7. {
    8.     public GameObject[] muzzleFlashes;
    9.     public GameObject[] bulletImpacts;
    10.     public GameObject[] bulletTracers;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         muzzleFlashes = Resources.LoadAll<GameObject>("Weapons/Weapon Muzzle Flashes");
    16.         bulletImpacts = Resources.LoadAll<GameObject>("Weapons/Weapon Bullet Impacts");
    17.         bulletTracers = Resources.LoadAll<GameObject>("Weapons/Weapon Bullet Tracers");
    18.  
    19.         foreach (GameObject flashes in muzzleFlashes)
    20.         {
    21.             var muzzleFlashesPool = new ObjectPool<GameObject>(() => new GameObject(this.gameObject.name), (obj) => obj.SetActive(true), (obj) => obj.SetActive(false), (obj) => Destroy(obj), false, 100, 200);
    22.         }
    23.  
    24.         foreach (GameObject impacts in bulletImpacts)
    25.         {
    26.             // SETUP BULLET IMPACTS POOL
    27.         }
    28.  
    29.         foreach (GameObject tracers in bulletTracers)
    30.         {
    31.             // SETUP BULLET TRACERS POOL
    32.         }
    33.     }
    34. }
    Once that's done, when the gun fires it will simply look for a pool that matches the name of the desired effect, and grab one from the pool.

    So I basically need muzzleFlashesPool to be gameObject.name , does anyone know how I would achieve this?.

    Thanks in advance,
    DPK
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yep, you can't. You can't rename variables in code at runtime. If your gameObject.name was, for example, "MuzzleFlashPoolSuperHolderOfDoom", you can't just have the variable suddenly be
    var MuzzleFlashPoolSuperHolderOfDoom = new ...

    If that's not what you meant, then please clarify. as I might be misunderstanding what you are actually doing.

    If your gameobjects all have different names, I'd suggest using a dictionary and using the gameObject.name as the key. If you have some that are the same, this isn't going to work since dictionaries must contain unique values for the keys.
     
    Kurt-Dekker likes this.
  3. DanielRiches

    DanielRiches

    Joined:
    Nov 23, 2019
    Posts:
    166

    Thanks, i'll look into a Dictionary then