Search Unity

Best Practices for passing parameters to prefabs

Discussion in 'Prefabs' started by froztwolf_unity, Mar 31, 2019.

  1. froztwolf_unity

    froztwolf_unity

    Joined:
    Nov 21, 2018
    Posts:
    20
    I've been wondering about best practices on spawning prefabs with parameters.

    I have a brickManager that is instantiated by my gameManager.
    After instantiating it, I pass down a parameter called "tileset" which contains the information on the bricks the brickManager should create.

    The brickManager is a Monobehaviour class, because it needs to instantiate objects.
    But because it can't do any useful work until the tilemap has been passed down from the gameManager, I can't use the Awake() or Start() methods, and if I want to use Update() or FixedUpdate() I need to encase everything in a conditional block on whether the tilemap is null.

    I have multiple instances of this in my project and it's cumbersome to the point that I feel like I must be missing some best practices here.

    I'd like to use a constructor with parameters to create the brickManager, but if it's not a Monobehaviour class I can't use it to instantiate objects, as far as I can tell.

    Better yet, I'd like it if there was a way to pass in my parameters before Awake() is called, but I can't see that there's any way to do that.

    I'd rather not create a brick manager prefab for every tileset I have. Kind of ruins the point of having a manager/factory create the bricks.

    Any advice?