Skip to main content
added 2 characters in body
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

A simple trick I like to use is introduce a self-counting object script:

public class CountedObject : MonoBehaviour {

    static int _count;

    void OnEnable() { _count++; }

    void OnDisable() { _count--; }

    public int GetActiveCount() { return _count; }
}

Then your spawner can look like...

public class CreatingCan: MonoBehaviour 
{
    public CountedObject canPrefab;
    public int spawnLimit = 3;
    public bool canSpawn = true;

    void Update()
    {
        // Don't spawn more if we've disabled spawning, or are at/above our limit.
        if (!canSpawn || canPrefab.GetActiveCount() >= spawnLimit)
            return;

        // Check all touches, not just the first one, so it works even if you
        // hold one finger on the screen, and tap with another.
        for (int i = 0; i < Input.touchCount; i++) {
            var touch = Input.GetTouch(i);
            if (touch.phase != TouchPhase.Began)
                continue; // Skip any old touches.

            Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            Instantiate(canPrefab, touchPosition, Quaternion.identity)

            // Abort if we hit our limit, even if there are still more touches.
            if (canPrefab.GetActiveCount() >= spawnLimit)
                break;
        }
    }    
}

A simple trick I like to use is introduce a self-counting object script:

public class CountedObject : MonoBehaviour {

    static int _count;

    void OnEnable() { _count++; }

    void OnDisable() { _count--; }

    public int GetActiveCount() { return _count; }
}

Then your spawner can look like...

public class CreatingCan: MonoBehaviour 
{
    public CountedObject canPrefab;
    public int spawnLimit = 3;
    public bool canSpawn = true;

    void Update()
    {
        // Don't spawn more if we've disabled spawning, or are at/above our limit.
        if (!canSpawn || canPrefab.GetActiveCount() >= spawnLimit)
            return;

        // Check all touches, not just the first one, so it works even if you
        // hold one finger on the screen, and tap with another.
        for (int i = 0; i < Input.touchCount; i++) {
            var touch = Input.GetTouch(i);
            if (touch.phase != TouchPhase.Began)
                continue; // Skip any old touches.

            Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            Instantiate(canPrefab, touchPosition, Quaternion.identity)

            // Abort if we hit our limit, even if there are still more touches.
            if (canPrefab.GetActiveCount >= spawnLimit)
                break;
        }
    }    
}

A simple trick I like to use is introduce a self-counting object script:

public class CountedObject : MonoBehaviour {

    static int _count;

    void OnEnable() { _count++; }

    void OnDisable() { _count--; }

    public int GetActiveCount() { return _count; }
}

Then your spawner can look like...

public class CreatingCan: MonoBehaviour 
{
    public CountedObject canPrefab;
    public int spawnLimit = 3;
    public bool canSpawn = true;

    void Update()
    {
        // Don't spawn more if we've disabled spawning, or are at/above our limit.
        if (!canSpawn || canPrefab.GetActiveCount() >= spawnLimit)
            return;

        // Check all touches, not just the first one, so it works even if you
        // hold one finger on the screen, and tap with another.
        for (int i = 0; i < Input.touchCount; i++) {
            var touch = Input.GetTouch(i);
            if (touch.phase != TouchPhase.Began)
                continue; // Skip any old touches.

            Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            Instantiate(canPrefab, touchPosition, Quaternion.identity)

            // Abort if we hit our limit, even if there are still more touches.
            if (canPrefab.GetActiveCount() >= spawnLimit)
                break;
        }
    }    
}
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

A simple trick I like to use is introduce a self-counting object script:

public class CountedObject : MonoBehaviour {

    static int _count;

    void OnEnable() { _count++; }

    void OnDisable() { _count--; }

    public int GetActiveCount() { return _count; }
}

Then your spawner can look like...

public class CreatingCan: MonoBehaviour 
{
    public CountedObject canPrefab;
    public int spawnLimit = 3;
    public bool canSpawn = true;

    void Update()
    {
        // Don't spawn more if we've disabled spawning, or are at/above our limit.
        if (!canSpawn || canPrefab.GetActiveCount() >= spawnLimit)
            return;

        // Check all touches, not just the first one, so it works even if you
        // hold one finger on the screen, and tap with another.
        for (int i = 0; i < Input.touchCount; i++) {
            var touch = Input.GetTouch(i);
            if (touch.phase != TouchPhase.Began)
                continue; // Skip any old touches.

            Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            Instantiate(canPrefab, touchPosition, Quaternion.identity)

            // Abort if we hit our limit, even if there are still more touches.
            if (canPrefab.GetActiveCount >= spawnLimit)
                break;
        }
    }    
}