So recently, I have tried to create a system that will detect when an object touches a cup, and then it will be stored in an array so that I can show it visibly and detect how many slots are holding something. The problem is that I don’t know how to detect how many spots in my array are being used, and then fill or deny filling the next slot. For example, lets say I let a tomato touch the cup, it will sense it, then fill the first spot in the array after it knows that currently, 0 spots are being used.
I currently have a system that detects whether the touching object is allowed to be stored, but don’t know how to figure out how many spots are being used. My array can store up to 3 GameObjects.
How can I detect how many spots are storing data in an array?
This is the first script I am currently using (creates the array):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IngredientStorer : MonoBehaviour
{
public GameObject[] SmallCupIngredientArray = new GameObject[3];
}
This is the second script I am using:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class IngredientDetection : MonoBehaviour
{
public IngredientStorer ingStore;
public GameObject ingAdded;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == 7)
{
Debug.Log("Ingredient detected");
ingAdded = other.gameObject;
if (ingStore.SmallCupIngredientArray. == 0) //Don't know what to put here to sense how many spots are taken in the array
{
ingStore.SmallCupIngredientArray[0] = ingAdded;
Debug.Log("Ingredient " + ingAdded.name + " added to array slot 0/2");
}
}
}
}
Thanks!
As you can probably tell, i’m not the greatest at Unity, and help would be greatly appreciated.
Thanks, I think that using a List will definitely work better for me.
– AceParker