1
\$\begingroup\$

We are developing a 2D game for Android on Unity. It's our first project, we don't have a lot of knowledge on Unity. We have 2 scenes, in first scene there is only a "Play" button. When that button clicked, other level loads.

void Update () {
    startButton.onClick.AddListener(() =>
    { 
       Application.LoadLevel(1);
    });
}

Problem is; when we click on Play button, it takes at least 10 seconds to load other level. There isn't much objects on other level so it shouldn't take that much time to load. Sometimes it takes 20 seconds and sometimes game freezes and level won't load. How can we reduce that loading time?

\$\endgroup\$
0
\$\begingroup\$

You should read the documentation about AddListener(). It adds the specified argument as callback to the onClick event.

That means, once you call the function with your callback, it will be registered and called, whenever the event is fired. Since Update() is called once per frame, you keep adding the callback to the event, which will result in hundreds of calls to it, once the event is being triggered.

In order to fix this, you can simply move the call to AddListener() to the Start or Awake function, which is only called once per GameObject.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.