Problem with prefab

Hi, i have a problem with adding GameManager with hierarchy to a prefab which isnt in hierarchy, its create while game. When i created prefab with GameManager i can add this, but its impossible to have two gamemanager :>

So, any ideas guys?

thanks!

This depends on your script. If you use the singleton pattern you have code that restricts 2 gamemanagers probably

Since Prefabs are defined outside/independent of scenes, they cannot be directly connected with something in the scene before the game starts.
Singletons are usually the way to go, but be careful to not overuse that pattern. In general try to minimize dependencies between scripts.

If you need something like a data storage, look into ScriptableObjects. Those are defined similar to prefabs outside of the scene and thus can be referenced in a prefab like any other asset.

Am not sure what you mean by “its impossible to have two gamemanager”.

1 Like

Just to interrupt…Thanks DragonCoder for your explanation of ScriptableObjects. They are more or less hidden away in Unity and not explained very well. Good on you for bringing this up.

I have to use prefab becuase i maked popup damage information with animation create with click. In this prefab is script which have OnDestroy event and in this event is money text which set money + amount.

When player click on the “monster” instantiate prefab popup money animate to money text and when it’s destroy by time its set text in other script which is on the GameObject gameManager

And in game manager is TMPro :

and public method to set money text :

So in popup money prefab i have script to this which have GameObject of game manager :

So how i can add here(up) object of game manager when popup money is create?

Sorry for my english :smile:

Thanks

When i make gameManager as a prefab and add this to popup money is ok, method calling but still game manager prefab dont have element of money text

Ok guys everything is fine, i used PlayerPrefs to get and set money increase, so topic closed!