#Communicating between scenes

1 messages · Page 1 of 1 (latest)

narrow sorrel
#

Hello everyone, I just separated my game into separate scenes and have them added via persistent scene which contains a scene manager script. What I don't understand how I can communicate between the scenes. For example, one has a healthbar. I had it setup/working using Unity actions which, based on my research, seemed like the correct approach. However, I implemented by attaching references to the scripts and accessing the actions via that. Now, as far as I can tell, I can't attach references to the scripts because they are in different scenes. I would appreciate any help you could provide.

frail copper
narrow sorrel
#

Hmm...so my scene manager and my audio manager are currently setup as singletons. So, I could add another one that is for events that allows everyone to communicate with it? That sounds pretty easy. I don't mind doing bootstrapping or whatever if its considered better practice. I just know when I try to drag/drop the scripts from separate scenes in the editor (even when they are both open) it won't allow it.

frail copper
narrow sorrel
#

Ok. I'm going to add a event manager right now.

#

Thanks for your thoughts. I'm going to leave unsolved just in case someone else wants to chime int.

narrow sorrel
#

@nav : Any chance you are still around? I can't get this working. I have a persistent scene wuth a scene controller and an event controller. I'm trying to setup the scene manager to be able to update a loading bar on a different scene (loading) which it has brought it. I don't think the details matter much because its all as barebone as it can get.

#

From scene controller I am running...

EventController.Instance.OnLoadingBarUpdate?.Invoke(progress);

gives me the following error

Member 'EventController.OnLoadingBarUpdate' cannot be accessed with an instance reference; qualify it with a type name instead
#

This

EventController.OnLoadingBarUpdate?.Invoke(progress);

gives me

The event 'EventController.OnLoadingBarUpdate' can only appear on the left hand side of += or -= (except when used from within the type 'EventController')
#

and here is my eventController class

#
public class EventController : MonoBehaviour
{
    // Singleton
    public static EventController Instance;
    // Events
    public static event Action<int> OnLoadingBarUpdate;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }
}
#

As far as I can tell I can't invoke an Action event from outside the EventController itself which makes it useless for my goal of communicating between scenes. Hopefully I'm missing something obvious.

frail copper
#

you normally just need to register some listeners through injection / event.

#

can you explain a practical usecase you want to do, not sure I understood what you mean with the LoadingBar thing

narrow sorrel
#

@frail copper : What you said makes sense. I think I had things messed up in my head. Can I just ask, on a high level, how to handle the following situation?

#

My plan was to have an inventory scene. It will have a button to drop an item. I don't get how communication can happen from the inventory scene to the game environment scene.

#

Neither of which are singletons or part of the persistent scene.

frail copper
narrow sorrel
#

At this stage I am completely learning and nothing is set

#

I just switched everything over to seperate scenes and learning the pain points

#

I just want to do it the best I can.

#

And of course, I know there is no best way/the best way depends on the game...but I'm talking more in principle.

frail copper
#

tbh the best way is always something that comes easy for you to do, and works..

you learn better ways as you go.. like you said.. you try something out, doesn't work well, you go back and find a better way.

narrow sorrel
#

Would you be willing to discribe a good approach to the inventory situation. No restrictions?

frail copper
#

in what way? this is a bit of an open ended question..
there are several, each project has its own requirements

narrow sorrel
#

I can open a new thread when I have specific questions. I really appreciate your time/thoughts. I think I'm going to merge things down to only two scenes at a time and limit communication to simple event actions for now.