Search Unity

Trying to figure out how to make a script that counts "coins" then destroys a wall

Discussion in 'Scripting' started by XYX1993XYX, Jun 18, 2021.

  1. XYX1993XYX

    XYX1993XYX

    Joined:
    Oct 2, 2019
    Posts:
    14
    So basically im looking how to write a script that counts coins picked up then destroys a wall after a certain amount is picked up!
     
  2. katyallol99

    katyallol99

    Joined:
    Jun 18, 2021
    Posts:
    4
    I would just use a int/float to keep track of the coins picked up. Then make it so that if the int/float == (amount you want to destroy wall) then just say Destroy(wall). I would also make a public gameObject variable (named wall, for example) that can be assigned in the inspector.
     
  3. katyallol99

    katyallol99

    Joined:
    Jun 18, 2021
    Posts:
    4
    I can give a C# example of what I said above if u want
     
  4. XYX1993XYX

    XYX1993XYX

    Joined:
    Oct 2, 2019
    Posts:
    14
    please do!
     
  5. katyallol99

    katyallol99

    Joined:
    Jun 18, 2021
    Posts:
    4
    Here you go:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4.  
    5. public class WallDestroyer : MonoBehaviour
    6. {
    7. public GameObject wall;
    8. public float coinsCollected;
    9. public float amountToDestroyWall;
    10.  
    11. void Update()
    12. {
    13. if (coinsCollected == amountToDestroyWall)
    14. {
    15. Destroy(wall);
    16. }
    17. }
    18.  
    19. void OnCollisionEnter(Collision collision)
    20. {
    21.  
    22. if (collision.gameObject.CompareTag("Coin")
    23. {
    24. coinsCollected += 1;
    25. }
    26.  
    27. }
    28.  
    29. }
    30.  
     
  6. XYX1993XYX

    XYX1993XYX

    Joined:
    Oct 2, 2019
    Posts:
    14


    thank you!
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    In cases like these always make it a >= check. The problem with == is that you may skip over the exact value.
    Imagine falling straight down on 2 coins, going directly from one below the desired value to one above it in the same frame. The condition would not trigger. Using >= instead the condition would always trigger once as soon as the required amount was picked up.
    In this specific case, the case described above is practically bound to happen sooner or later if the level design allows for coins to be placed within one player collider size apart from each other, so it is a quite likely scenario even.

    While the above is a general best practice to keep in mind, another way to approach the problem is to use properties instead. Declaring a property for a variable lets you define behavior that should happen when the variable is get or set. This would allow to check whether the condition is met each time a new value is set, which (for single-threaded applications) also prevents to skip over the condition. It would have the additional benefit of not requiring to run an Update function each frame. But those are implementation details.
     
    Last edited: Jun 18, 2021