Home>

I'm not very good at programming and I'm in trouble.
I want to acquire multiple items and move to the clear screen.

Even if you get an item, it will not be cleared.

Corresponding source code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item: MonoBehaviour
{
public static bool clear;
private int count;

private void Start ()
{
clear = false;
count = 0;
}

void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy (gameObject);
count + = 1;
if (count>= 5)
{
clear = true;
}
}
}
}

What I tried

You can move to the clear screen by making one instead of multiple acquisitions.

Supplementary information (FW/tool version, etc.)

Unity 2020.1.2f1

  • Answer # 1

    Isn't this script attached to each item?
    In that case, it seems that the variable count, which counts how many items were taken, is different for each item and is not counted up accurately.

    How about counting the items acquired on the player side instead of the item side?