Home > Software engineering >  How to control multiple GameObjects
How to control multiple GameObjects

Time:04-24

I'm currently in the process of making my first bigger game, and I have a problem. I have already searched a lot for answers, but I haven't been successful.

My problem is, that I want to make multiple doors that the player can open individually on the map (there will bee around 40 doors). And I feel like there must be a better way of controlling multiple GameObjects than to drag and drop in separate variables. I have played around with GameObjects arrays and .find, but I still feel like there must be af better way.

Thanks in advance :-)

CodePudding user response:

If all doors can be freely opened without any checks, the easiest way would be creating a script that inherits MonoBehaviour and assign it to a prefab that you use to create the doors.

If the doors are already there, you can select all of them at once on the hierarchy and add this door component to all of them at the same time.

To avoid the need of dragging and droping every single variable of the door script manually you can do it on the door script itself.

This is a script assuming you use an animator to open the doors, if not just replace the Open() method with what you actually use.

The script contains two different ways of assigning those variables in the editor. 1: Using the Reset() event function, which is called whenever you add this script inside the editor or when you click "Reset" on the context menu.

2 (My preferred): Using the OnValidate() event function, which is called whenever you load the gameobject on the editor or when you edit any value on the inspector.

public class DoorScript : MonoBehaviour
{
    [SerializedField] private Animator animator;

    public void Open()
    {
        animator.Play("DoorOpenAnimation", 0f);
    }

    private void Reset()
    {
        animator = GetComponent<Animator>();
    }

#if UNITY_EDITOR // we can't let this compile on game builds because the UnityEditor namespace does not exist on builds (which we are using bellow)

    private void OnValidate()
    {
        if (animator != null) // if we already have the animator we don't need to do anything
            return;

        animator = GetComponent<Animator>();
        if (animator == null) // if we fail to get the animator we log a warning to let you know
            Debug.LogWarning($"Could not find animator on {gameobject.name}!", this); 
        else
            UnityEditor.EditorUtility.SetDirty(this); //this sets this gameobject as dirty and is needed to force the Editor to save the changes we made when you click "Save"
    }

#endif }

CodePudding user response:

What language are you using? The following is a general template you can use, as long as it is object-oriented.

Create a Door class. Make each door an object of class door. Define class door to have an attribute that keeps track of state such as opened = false. It's also good practice to make getter and setter methods:

function open() {
   opened = true;
}

This way, you can easily change the state of doors.

Door d = new Door();
d.open();

You could keep track of the doors using an ArrayList if you wanted.

ArrayList<Door> doors = new ArrayList<Door>();
doors.add(d);
doors.get(0).close();
  • Related