Search Unity

Question Problem with prefab

Discussion in 'Getting Started' started by ShuffleTV, Sep 24, 2022.

  1. ShuffleTV

    ShuffleTV

    Joined:
    Sep 1, 2022
    Posts:
    4
    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!
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,059
    This depends on your script. If you use the singleton pattern you have code that restricts 2 gamemanagers probably
     
  3. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    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".
     
    vertexx likes this.
  4. vertexx

    vertexx

    Joined:
    Mar 18, 2014
    Posts:
    379
    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.
     
  5. ShuffleTV

    ShuffleTV

    Joined:
    Sep 1, 2022
    Posts:
    4
    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 :D

    Thanks
     
  6. ShuffleTV

    ShuffleTV

    Joined:
    Sep 1, 2022
    Posts:
    4
    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
     
  7. ShuffleTV

    ShuffleTV

    Joined:
    Sep 1, 2022
    Posts:
    4
    Ok guys everything is fine, i used PlayerPrefs to get and set money increase, so topic closed!