Search code examples
eventsunity-game-engineevent-handlinggame-engine

Create EventSystem from script


I'm writing a Unity editor script and need to make sure that an (UI) Event System exists so I want to create one if it's not already there. But the EventSystem class and StandaloneInputModule class can nowhere be found when trying to import it to a script. What's the matter with this? I can't find any other info on this matter either.


Solution

  • When you add an UI item, the EventSystem object is automatically added. Just drag it into your Project to make it a prefab so you can use it to be instantiated just like any game object.

    public GameObject eventPrefab;
    void Start(){
        if(GameObject.Find("EventSystem") == null){
             Instantiate(eventPrefab);
        }
    }