- Home /
List becomes null in Parent when filled in child script,List becomes null within own class after being filled in other class
Dear all,
I am trying to fill a list of Gameobjects that I spawn every X seconds. This list is instantiated in one class (PickUpWeapons) as such:
public List<GameObject> weaponsToPickUp = new List<GameObject>(); //list of weapons spawned over the course of the game
And in my class where I spawn weapons (SpawnObjects), I fill this list as such: PickUpWeapons pickUp; //reference to pickUpWeapons class
//instanciate
private void Start() {
//generate instance of the pool
objectPooler = ObjectPooler.Instance;
pickUp = gameObject.AddComponent<PickUpWeapons>(); //instanciate the class
//pickUp.weaponsToPickUp = new List <GameObject>();
}
private void FixedUpdate() {
//only spawn objects if it is time
if (Time.time > nextSpawnTime) {
//update next spawn time
nextSpawnTime = Time.time + spawnRateWeapons;
//where we spawn the weapons
float randomXPos = Random.Range(- xMax, xMax);
whereToSpawn = new Vector2(randomXPos, transform.position.y);
//generate object
GameObject bullet = ObjectPooler.Instance.SpawnFromPool("Bullet", whereToSpawn, Quaternion.identity);
//freeze its position on X only
bullet.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
//add weapon generated as a potentially picked up weapon
pickUp.weaponsToPickUp.Add(bullet);
Debug.Log(pickUp.weaponsToPickUp.Count);
}
}
}
In this class (SpawnObjects) pickUp.weaponsToPickUp is not empty and the Log does indicate an increase in size of weaponsToPickUp. However, back in PickUpWeapons class, this list is now empty.
Does anyone know what I could do to solve this please?
Best Regards, Anthony Guillard
,Dear all, I have the two following classes, one that creates items and the other that enables the user to pick them up.
In my class PickUpWeapons, I create a list of GameObjects
public List<GameObject> weaponsToPickUp = new List<GameObject>(); //list of weapons spawned over the course of the game
Which is filled in the class SpawnObjects
PickUpWeapons pickUp; //reference to pickUpWeapons class
//instanciate
private void Start() {
//generate instance of the pool
objectPooler = ObjectPooler.Instance;
pickUp = gameObject.AddComponent<PickUpWeapons>(); //instanciate the class
pickUp.weaponsToPickUp = new List <GameObject>();
}
private void FixedUpdate() {
//only spawn objects if it is time
if (Time.time > nextSpawnTime) {
//update next spawn time
nextSpawnTime = Time.time + spawnRateWeapons;
//where we spawn the weapons
float randomXPos = Random.Range(- xMax, xMax);
whereToSpawn = new Vector2(randomXPos, transform.position.y);
//generate object
GameObject bullet = ObjectPooler.Instance.SpawnFromPool("Bullet", whereToSpawn, Quaternion.identity);
//freeze its position on X only
bullet.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
//add weapon generated as a potentially picked up weapon
pickUp.weaponsToPickUp.Add(bullet);
}
}
}
But when I access again the list weaponsToPickUp in PickUpWeapons (its class), the list is null/empty and does not contain the bullets spawned anymore which is problematic as I cannot detect the collisions afterwards.
Does anyone know how to solve this please?
Best Regards, Anthony Guillard
Your answer
Follow this Question
Related Questions
Array of Lists null exception when adding to list. 2 Answers
How to rearange the order of a list after an item was removed? 0 Answers
Unable to delete object from a list using foreach loop 1 Answer
looping through game objects 1 Answer
How can i change the scriptable object variable in child script (if it's possible) 0 Answers