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

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 :

public class lootBoxScript : MonoBehaviour
{
    public float timer = 5f;  // timer 
   
    GameObject lootbox;
    public MeshRenderer MR;  // for getting the gameobject MeshRendrer
    public BoxCollider BC;        // for getting the gameobject BoxCollider
    public bool isTouch = false;  // when the box is touchable 
    public bool isRepeat = false;  // to be able to let the box "Re-do" the disappering


    private void OnCollisionEnter(Collision collision)
    {
        // when the player touch the box, the box looses it Meshrender & Collider
        if (collision.collider.tag == "Player")
        {
            MR.enabled = false;
            BC.enabled = false;
            isTouch = true;
        }
    }

    private void Update()
    {
        if(isTouch)
        {
            timer -= Time.deltaTime;
           
            if(timer <= 0)
            {
                isRepeat= true;
            }
         }
         
        if(isRepeat.Equals(true))
        {
            MR.enabled = true;
            BC.enabled = true;
            isTouch = false;
            timer = 5f;
        }

    }

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

if(isRepeat.Equals(true))
        {
            MR.enabled = true;
            BC.enabled = true;
            isTouch = false;
            timer = 5f;
            //add this line
            isRepeat = false;
        }

but i think you’re searching

private void Update()
    {
        if(isTouch)
        {
            timer -= Time.deltaTime;
      
            if(timer <= 0)
            {
               if (isRepeat)
               {
                    MR.enabled = true;
                    BC.enabled = true;
                    isTouch = false;
               }
               timer = 5f;
            }
         }
    
    }

Coding stuff… never use like this:
timer = 5f;
you should have a constant in your code:

const int timeoutRefresh = 5f;

... then use...

timer = timeoutRefresh;
1 Like

Dear adi7b9,

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

Kind regards