I’m making some supermarket kinda game, in which my character (only first person and basket) can collect any object from the supermarket in that basket.
Is there anyway to get the names of all objects that had been placed in that basket when the character reaches some check point?
Well, getting by position would really affect performance, you can get all the children from a gameobject like this:
public GameObjecy basket;
private void Start(){
Transform[] itemsInBasket = basket.GetComponentInChildren<Transform>(); // All childreen gameobjects of basket
Debug.Log(itemsInBasket[0].name) //Say the name of the first item.
}
The problem is that this would include the children of the children, an better way is this one:
public List<GameObject> basketItems = new List<GameObject>();
public GameObject banana, apple;
public void Start(){
basketItems.Add(banana); // Add some stuff
basketItems.Add(apple);
Debug.Log(basketItems[0].name); // Says "banana"
}
Control your basket items with that list, they will act like an normal gameobject, you can destroy change their position, etc.