0
\$\begingroup\$

In my game, when I instantiate a particular prefab, I need to show a GUI window, then let player do some actions, finally close that window and update the caller GameObject with the info inserted from user.

So my question is: how to save the reference of the GameObject caller to update it at the end of the user action.

Consider that I call some sub functions and I don't want to pass a GameObject through all functions.

Is there a way to save a gameObject reference in a global variable ? I'm using a static class for that purpose.

\$\endgroup\$
3
\$\begingroup\$

C# doesn't allow global variables (i.e.: variables outside classes).

The only way to mimic a global variable is to use a static class, as you already did.

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

What's wrong with passing a GameObject reference through these functions?

You need it at the end and the only place you can get it is the previous invoker in your chain of methods. When you instantiate the UI, pass the GameObject to it, store it in the UI handler class, and when do, do the update thing you need to on that saved reference.

\$\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.