I’m building a 2D game for mobile and just wondering if there’s a better way to change the values of a script attached to the pooled objects.
Here is the script attached to my player Object.
// PROJECTILE VARIABLES
public GameObject projectile;
public float fireRate;
private float nextAttack;
public int maxProjectiles;
private GameObject[] pool;
// END PROJECTILE VARIABLES
void Start () {
// INITIALIZE PROJECTILES
pool = new GameObject[maxProjectiles];
for (int i = 0; i < pool.Length; i++) {
pool *= Instantiate(projectile);*
_ pool*.SetActive(false);_
_ }_
_ // END INITIALIZE PROJECTILES*_
* }*
* void Update () {*
// FIRE PROJECTILE
Projectile();
// TEST CHANGING FLOAT “DECAY” IN POOLED OBJECTS SCRIPT
if (Time.time > 1.5f) {
for (int i = 0; i < pool.Length; i++) {
pool*.GetComponent<projectile_Controls>().decay = 0.1f;
_ }
}
// END TEST*
}_
The pooled objects have a different script projectile_Controls.cs attached which contains a float decay (among other things) that controls how long a projectile stays on screen.
The reason why I need to access all the pooled objects variables is because my player will pick up different kinds of weapons throughout the game. Since I’m pooling the projectiles at the start of the game, that means I will have to change the pooled projectiles textures and variables that control speed etc. when the player acquires a new weapon.
I’d like to know how I can access the variables of the pooled objects and change them without affecting performance too much.
Another way would be to simply create a pool for each weapon type but I think that will hurt memory, especially if I decide to have a very big selection of weaponry.
What do you suggest?
Oh and the player could be changing weapons quite often. Assume every 1-10sec.