Adding to a List and putting it through a function?

I watched the tutorials regarding Lists and Dictionaries but it was a bit confusing to me as I have never worked with classes before. I am sure the Unity Community is willing to help me out here.

I have set up two scripts, one for constructors and some functions, one for adding items to the list. My idea here is the script that adds to the list is like sending an ingredient to the pot. And the script that puts the items in the list through a function is like **cooking the ingredients inside the pot. **

Like:

public var ingredients = new List.<ingredientsToCook>();

function OnTriggerEnter (Col : Collider) {

    if(Col. CompareTag("Pot"))
    {
        ingredients.Add("Potatoe", 60)
    }
}

This will be in a public class called something like Ingredients.

And inside the pot:

public class items {

    var itemName : String;
    var cookTime : int;

    public function items(newName : String, newTime : int){

        itemName = newName;
        cookTime = newTime;
    }

    for(var ingredient in ingredients)
    {
         //cooks the ingredients
    }

}

Does this concept work? But what if each ingredient has a different time to cook? how do I access each item’s different var timeToCook : float = x inside a list and cooks the food accordingly? I will look at some youtube videos in the meantime.

thanks so much.

I think this is probably more what your looking to do:

 public class Pot : MonoBehaviour {
	List<Ingredient> ingredients = new List<Ingredient>();
     
	void OnTriggerEnter(Collider collider) {
		if (collider.CompareTag("Pot")) {
			ingredients.Add(new Ingredient("potato", 60));
		}
	}

	public void Cook() {
		for (int i = 0; i < ingredients.Count; ++i) {
			//whatevs
		}
	}
}

public class Ingredient {
	string _mame;
	string _time;
 
	public Ingredient(string name, int time) {
		_mame = name;
		_time = time;
	}
}

It seems like you are using JavaScript, which is not my string suit, but hopefully I’ll get most of it right

Your for loop is not going to work right there. You want to put that inside of a function - probably on Update(). It would look something like this:

function Update()
{
    for (var ingredient in ingredients) {

        //Have a variable on each ingredient that says whether it has been cooked yet. If it has been cooked, continue to the next item
        if (ingredient.Cooked)
            continue;

        //Have a variable on the ingredients to track how long they have been cooking
        ingredient.TimeSpentCooking += Time.deltaTime;

        if (ingredient.TimeSpentCooking >= ingredient.TimeToCook){
            ingredient.Cooked = true;
        }
    }
}