Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Creating a loot box (such as Mario Kart item boxes)

Discussion in 'Scripting' started by Greg_Al93, Dec 27, 2019.

  1. Greg_Al93

    Greg_Al93

    Joined:
    Jul 24, 2019
    Posts:
    2
    Hi,

    I am new to all the c# scripting and everything.
    As the title says, I am trying to create a loot box.
    What i want is to be able to touch the box, making it disappear, then when the timer go to 0
    the box reappear and do the same stuff each time I touch it.

    this is my script :

    Code (CSharp):
    1. public class lootBoxScript : MonoBehaviour
    2. {
    3.     public float timer = 5f;  // timer
    4.    
    5.     GameObject lootbox;
    6.     public MeshRenderer MR;  // for getting the gameobject MeshRendrer
    7.     public BoxCollider BC;        // for getting the gameobject BoxCollider
    8.     public bool isTouch = false;  // when the box is touchable
    9.     public bool isRepeat = false;  // to be able to let the box "Re-do" the disappering
    10.  
    11.  
    12.     private void OnCollisionEnter(Collision collision)
    13.     {
    14.         // when the player touch the box, the box looses it Meshrender & Collider
    15.         if (collision.collider.tag == "Player")
    16.         {
    17.             MR.enabled = false;
    18.             BC.enabled = false;
    19.             isTouch = true;
    20.         }
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         if(isTouch)
    26.         {
    27.             timer -= Time.deltaTime;
    28.            
    29.             if(timer <= 0)
    30.             {
    31.                 isRepeat= true;
    32.             }
    33.          }
    34.          
    35.         if(isRepeat.Equals(true))
    36.         {
    37.             MR.enabled = true;
    38.             BC.enabled = true;
    39.             isTouch = false;
    40.             timer = 5f;
    41.         }
    42.  
    43.     }
    with that code : the box disappear, timer go to 0, the box reappear BUT if i touch it again it do not diseappear.

    can someone help me?

    Kind regards
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1. if(isRepeat.Equals(true))
    2.         {
    3.             MR.enabled = true;
    4.             BC.enabled = true;
    5.             isTouch = false;
    6.             timer = 5f;
    7.             //add this line
    8.             isRepeat = false;
    9.         }
    but i think you're searching

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if(isTouch)
    4.         {
    5.             timer -= Time.deltaTime;
    6.      
    7.             if(timer <= 0)
    8.             {
    9.                if (isRepeat)
    10.                {
    11.                     MR.enabled = true;
    12.                     BC.enabled = true;
    13.                     isTouch = false;
    14.                }
    15.                timer = 5f;
    16.             }
    17.          }
    18.    
    19.     }

    Coding stuff.. never use like this:
    Code (CSharp):
    1. timer = 5f;
    you should have a constant in your code:
    Code (CSharp):
    1. const int timeoutRefresh = 5f;
    2.  
    3. ... then use...
    4.  
    5. timer = timeoutRefresh;
     
    Last edited: Dec 27, 2019
    Greg_Al93 likes this.
  3. Greg_Al93

    Greg_Al93

    Joined:
    Jul 24, 2019
    Posts:
    2
    Dear adi7b9,

    thanks to your advice i managed to do what I wanted !

    Kind regards