Home > Mobile >  How do I attach a gameObject to objects created in script with class definition?
How do I attach a gameObject to objects created in script with class definition?

Time:07-23

I am confused about how to link a gameObject to an object created in its attached script. I have created two gameObject tables. My goal was to put those tables in a List or array, cycle through the array to see if they have collided with a customer gameObject, and if so, remove them from the list of tables a customer can move to.

To do this, in the script I wanted the tables to have a bool field "isOccupied", so I created a Table class with this field, and created Table objects in the Start() method. Previously, I was using gameObject.FindGameObjectsWithTag("Table") method to add the gameObject tables to an array, but now I am confused about how to link the tables with object creation, and I am pretty sure I'm fundamentally misunderstanding something.

Essentially, I am wondering:

  1. Do I need to create objects in script if those gameObjects already exist in Unity?
  2. Is class creation unnecessary in this case/ are there more suitable ways to go about defining fields & behaviors for gameObjects?

Any advice or explanations would be really appreciated.

Here is the code:

public class TableBehavior : MonoBehaviour
{
    [SerializeField] public List<Table> tableList = new List<Table>();
    public bool isOccupied;
    public int tableNumber;

   //Start is called before the first frame update
    void Start()
    {
        //exampleList = gameObject.FindGameObjectsWithTag("Table");
        
        //initializing 3 tables with table numbers (unused), and isOccupied field set to false
        Table t1 = new Table(0, false);
        Table t2 = new Table(1, false);
        Table t3 = new Table(2, false);

        tableList.Insert(0, t1);
        tableList.Insert(1, t2);
        tableList.Insert(2, t3);
    }

    // Update is called once per frame
    void Update()
    {
        isTableOccupied();
    }
   
    // if table is occupied, remove it from list of options customer can move to
    public void isTableOccupied()
    {
        foreach(Table t in tableList)
        {
            if (t.isOccupied == true)
            { 
                tableList.Remove(t);
            }         
        } 
    }

    // i want this method to iterate through tableList, and if a collision is detected in game with customer, set isOccupied field to true
    void OnCollisionEnter2D(Collision2D collision)
    {   
        foreach(Table t in tableList)
        {
            if(collision.gameObject.tag == "customer")
            {
                Debug.Log("collision detected!");
                t.isOccupied = true;
                        
            }
        
        }
    }
}

CodePudding user response:

You are right to think that you might be misunderstanding something. But to be fair, classes (or rather MonoBehaviour classes) in Unity tend to work quite differently in Unity than in "pure" C# environments.

The first weird thing is to never use the constructor of MonoBehvaiour drived classes. The main ways to create objects is:

For your specific case, I would not use a separate Table class, but rather put everything in your TableBehaviour. Then have another MonoBehaviour class like TableManager that Instantiates the tables from a Prefab. You can have the coordination code in this TableManager class, but the Rest like collision in the TableBehvaiour class.

To learn the general concepts of Unity I can recommend this short tutorial series.

  • Related