Search Unity

Loop generating infinte amount of items - no clue why

Discussion in 'Scripting' started by misterG420, Mar 18, 2020.

  1. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hello unity community,

    I have a serious logic problem with my current snippet of code. A bit of explanation before I copy-paste the code:

    I have created a Bakery, which generates an "Item" each time I place a sack of flour (FlourSack) and a bucket full of water (BucketWater). If I place either item into the area, the bool for that ingredient turns true. Then in the update function I want to instantiate the "item" only when both bools are true (so if both items are present in the trigger area). I thought a "for (int = 0; i....)" loop would make sense. My idea was to instantiate only 4 items. But I either generate 2 items (if I set the second if statement to "amountOfBakedToast >=4), or an unlimited amount of that item. Please note that I've currenlty set the amountOfBakedToast to 4 in the unity itself as a max of items generated. What did I do wrong, why can't it instantiate exactly 4 piece of item (=toast).



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

    public class Bakery : MonoBehaviour
    {
    public GameObject prefab;
    public int amountOfBakedToast;
    private bool ingredientsFlour;
    private bool ingredientsWater;


    private void Start()
    {
    ingredientsFlour = false;
    ingredientsWater = false;
    }

    private void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag("FlourSack"))
    {
    ingredientsFlour = true;
    }

    if (other.CompareTag("BucketWater"))
    {
    ingredientsWater = true;
    }
    }

    private void Update()
    {


    if (ingredientsWater == true && ingredientsFlour == true)
    {

    Destroy(GameObject.FindWithTag("FlourSack"));
    Destroy(GameObject.FindWithTag("BucketWater"));

    for (int i = 0; i < amountOfBakedToast; i++)
    {
    Instantiate(prefab, new Vector3(-2.95f, 1.16f, -20.2f), Quaternion.identity);
    Debug.Log("Here is your Toast, good sir!");

    if(amountOfBakedToast >=4)
    {
    //does for some reason only instantiate 2 toasts... if I make >=5 it instantiates unfinite amount of items
    Destroy(GameObject.FindWithTag("Item"));
    break;
    }
    }


    }
    }




    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Where are you setting ingredientsWater and ingredientsFlour back to false? Otherwise it is going to try to instantiate that prefab on every Update afterward, unless you're getting runtime errors when you try to Destroy(null) when those FindWithTag calls don't find anything.

    Also, use CODE tags when you post code.
     
    Last edited: Mar 18, 2020
  3. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Dear Joe, thanks that was it! I added:


    if(amountOfBakedToast >=4)
    {
    ingredientsFlour = false;
    ingredientsWater = false;
    }

    and now it instantiates exactly 4 :) you are a hero!
     
    Joe-Censored likes this.