Skip to main content
Missing parenthesis (that'll teach me to answer with the mobile app)
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountedPickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountedPickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountedPickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

Correcting inconsistent naming
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountablePickupCountedPickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountablePickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountedPickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

I'd be inclined to do this with a static variable on a script attached to your pickups:

public class CountedPickup : MonoBehaviour { static int _pickupsRemaining; public static int PickupsRemaining { get { return _pickupsRemaining; } void OnEnable() { _pickupsRemaining++; } void OnDisable() { _pickupsRemaining--; } } 

Now your player script (or any other) can check CountablePickup.PickupsRemaining == 0 to check how many are left to collect.

This will work even if this component doesn't know anything about what kind of pickup it is (it just represents the idea that this pickup should be included in the count, even if you mix & match different types of pickups in one level), and whether you Instantiate() & Destroy() pickups, or just toggle them on and off with gameObject.SetActive()