How to get an object from a list ?

Jakub Cernicky

New member
Joined
Jun 7, 2022
Messages
3
Programming Experience
Beginner
hello I'm doing a game and I'm stuck here, I have a collider box created and if I put a bucket in the collider box, it will be added to the List and when I put it away from the collider box it will be removed from List but I need it to fill with dirt in that collider box and I don't know how I have get the definition of this bucket from that sheet so that it only works for that one bucket in the collider box

I'll upload the script I have here

Thank you for your help

My list code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SiveController : MonoBehaviour
{
    public GameObject EmptyBucket;
    public GameObject BucketOfWater;
    public GameObject BucketOfDirt;
    public GameObject Dirt;

    List<GameObject> currentCollisions = new List<GameObject>();

    void updateItems()
    {
        // I don't know how to get the definition of a bucket that is added in a list
        //                                ↓
        if (currentCollisions.Contains(        ))
        {
            Debug.Log("EmptyBucket Enter");
        }

        if (currentCollisions.Contains(Dirt))
        {
            Debug.Log("Dirt Enter");
        }

        // I don't know how to get the definition of a bucket that is added in a list
        //                                                                        ↓
        if ((currentCollisions.Contains(Hlina)) && (currentCollisions.Contains(        )))
        {
            Debug.Log("Filling the Bucket");

            EmptyBucket.gameObject.SetActive(false);
            BucketOfWater.gameObject.SetActive(false);
            BucketOfDirt.gameObject.SetActive(true);

            currentCollisions.Clear();
        }
    }


    void OnTriggerEnter(Collider Other)
    {
        currentCollisions.Add(Other.gameObject);

        Debug.Log("Entering" + Other.gameObject.name);

        updateItems();
    }

    void OnTriggerExit(Collider Other)
    {

        currentCollisions.Remove(Other.gameObject);

        Debug.Log("Leaving" + Other.gameObject.name);

        
    }

}
 
RE: How check if a list is empty

Wouldn't an empty list have 0 items in it? If only there were a way to see how many items there are in a list. Oh wait there is:

As for checking to see if an item is in a list, you would either need to check by reference, or you would have to inspect each item in the list and see if the attributes of the item match up with what you are looking for.

I've got a feeling that you jumped to quickly into C# and missed the fundamentals of what it means for C# objects being accessed by reference.
 
Hi everyone, I have already solved this problem

My list code solved:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SiveController : MonoBehaviour
{
    private GameObject Prazdny;
    private GameObject PlnyVoda;
    private GameObject PlnyHlina;

    public ParticleSystem yourParticleSystem;

    private List<kyblController> kyblCheck = new List<kyblController>();

    private List<Hcontroller> hlinaCheck = new List<Hcontroller>();
    // Update is called once per frame

    void Start()
    {
    
    }

    void Update()
    {

    }

    void updateItems()
    {
        Debug.Log("updateItems");

        for (int i = 0; i < kyblCheck.Count; i++)
        {
            if (kyblCheck[i].name != "Kybl")
            {
                Debug.Log("Kybl Not Ready");
            }
            else if (kyblCheck[i].name == "Kybl")
            {
                Debug.Log("Kybl Ready");
            }
        }

        for (int x = 0; x < hlinaCheck.Count; x++)
        {
            if (hlinaCheck[x].name != "Hlina")
            {
                Debug.Log("Hlina Not Ready");
            }
            else if (hlinaCheck[x].name == "Hlina")
            {
                Debug.Log("Hlina Ready");
            }
        }

        for (int x = 0; x < hlinaCheck.Count; x++)
        {
            if (hlinaCheck[x].name == "Hlina")
            {
                for (int i = 0; i < kyblCheck.Count; i++)
                {
                    if (kyblCheck[i].name == "Kybl")
                    {
                        Debug.Log("Plnim");

                        Prazdny.gameObject.SetActive(false);
                        PlnyVoda.gameObject.SetActive(false);
                        PlnyHlina.gameObject.SetActive(true);

                        yourParticleSystem.Play();
                    }
                }
            }
        }
    }


    void OnTriggerEnter(Collider collisionInfo)
    {
        if (collisionInfo.name == "Kybl")
        {
            kyblController other = collisionInfo.gameObject.GetComponent<kyblController>();

            if (other == null)
            {
                return;
            }

            Debug.Log("kybl found");

            this.kyblCheck.Add(other);

            Debug.Log("kybl add");

            this.updateItems();

            Prazdny = other.transform.GetChild(0).gameObject;
            PlnyVoda = other.transform.GetChild(1).gameObject;
            PlnyHlina = other.transform.GetChild(2).gameObject;
        }

        if (collisionInfo.name == "Hlina")
        {
            Hcontroller other2 = collisionInfo.gameObject.GetComponent<Hcontroller>();

            if (other2 == null)
            {
                return;
            }

            Debug.Log("hlina found");

            this.hlinaCheck.Add(other2);

            Debug.Log("hlina add");

            this.updateItems();
        }
    }

    void OnTriggerExit(Collider collisionInfo)
    {
        if (collisionInfo.name == "Kybl")
        {
            kyblController other = collisionInfo.gameObject.GetComponent<kyblController>();

            if (other == null) { return; }

            this.kyblCheck.Remove(other);

            Debug.Log("Kibl Remove");

            this.updateItems();
        }

        if (collisionInfo.name == "Hlina")
        {
            Hcontroller other2 = collisionInfo.gameObject.GetComponent<Hcontroller>();

            if (other2 == null) { return; }

            this.hlinaCheck.Remove(other2);

            Debug.Log("Hlina Remove");

            this.updateItems();

        }
    }
}
 
RE: How check if a list is empty

Wouldn't an empty list have 0 items in it? If only there were a way to see how many items there are in a list. Oh wait there is:

As for checking to see if an item is in a list, you would either need to check by reference, or you would have to inspect each item in the list and see if the attributes of the item match up with what you are looking for.

I've got a feeling that you jumped to quickly into C# and missed the fundamentals of what it means for C# objects being accessed by reference.
Thank for help
 
Back
Top Bottom