Make loop execute once then exit

Hey everyone.

My issue here is that I don’t know how to add “items” (blocks) to my inventory, one at a time. They all get added and destroyed in just one click. My first solution in theory would be to somehow exit the loop once executed once. The second one is just using if statements. I’ve tried to dabble with them both but haven’t found a way to make it work.

public class PickupItems : MonoBehaviour
{
    private Inventory inventory;
    public GameObject itemButton;

    private void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    }

    private void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

    if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0) && hit.transform.CompareTag("Item"))
        {
                for (int i = 0; i < inventory.slots.Length; i++)
                {
                    if (inventory.isFull[i] == false)
                    {
                        inventory.isFull[i] = true;
                        Instantiate(itemButton, inventory.slots[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
         }
    }
}

White blocks are the items. If clicked on once, they’re all added, destroyed and converted to a potion (intentional).

Any suggestions greatly appreciated. I’ve found several tutorials on inventory systems, but none focused on using the mouse with clicks…

Thanks!

I don’t understand the problem. You say that you want to add individual items to your inventory one-at-a-time when clicking on an item…

…But then say the script’s current behavior of adding all items at the same time when clicking on any one item is intentional:

These contradict each other.

1 Like

I believe the intentional bit refers to “destroyed and converted to a potion”.

@Godnoken , based on the behaviour you describe I believe you are placing this script on each of your white cubes. This is probably the wrong place for it because it means your code will execute once for each cube in your scene. I believe you want this code to execute only once, regardless of the number of cubes in the scene. Try attaching it to something that only exists once, like your inventory GUI.

Also note that your script calls Destroy(gameObject). This is the gameObject the script is attached to, not the gameObject your raycast hit. In order to use only the game object that is clicked, you want hit.collider.gameObject.

2 Likes

Yes, sorry about the confusion. eisenpony read it the way I meant.

You’re absolutely right about everything! All along I was looking in the wrong place… Using my inventory management as the script host solves the adding issue.
I was also looking for a way to change from Destroy(gameObject) to something specific with the raycast hit, but to no success with transform and the use of unique ID’s. Didn’t realise you could reach gameObject through the collider…

Thank you very much, means the world to me. This is the first time I’ve ever actually asked for help when learning how to code and make games. Getting stuck is what’s made me quit pursuing this dream 10 times over. Thanks!

Besides the grammar misunderstanding, your question was well formatted and researched based on your current skill level. You showed the code you had tried, explained the actual behaviour and compared that to what you expected.

Keep asking questions like this and you will find this forum very helpful and accommodating. Everyone here wants to see you succeed, so don’t quit when you get stuck!

2 Likes