Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check if public variables are not null in prefabs

Discussion in 'Scripting' started by Irina-Marina, Nov 25, 2012.

  1. Irina-Marina

    Irina-Marina

    Joined:
    Sep 16, 2012
    Posts:
    7
    In my project i have some prefabs with scripts on them and inside those scrips some public variables which link to other prefabs.
    The designer has to set all the links manually.
    I'd like to programmatically check that all links aren't null and write Debug.LogError if some are null so that the designer could immediately see if he has forgot something when the scene is loading.

    For usual objects I use Awake() method and inside it check if all relevant public variables aren't null. But in prefabs Awake is never called since it's never instantiated in the scene. Start or OnEnabled aren't called as well.

    Is there any method that is called on a prefab when the game scene is loaded where I could do the error check?

    If no, what alternative technique should be used here?
     
  2. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Do you plan on using this prefabs later? If so you can look into just creating a pool of these items. A pool is just when you instantiate a set number of prefabs at the start of the game and then hide it. When you need a Prefab of that type you simply just call the pool to activate it and then place it where you you want.

    I included my version of it. I'm sure their is a better one out there on the web if you look for Unity3d Pool Manager. Mine includes a Editor I wrote that you can just drag and drop your prefab onto the pool manager. From their you can set how many you want and set a Name that you can call or you can use the predefined Id(index number).

    To use it in your code all you have to do is _PoolManager.Instance.ActivatePooledItem(Id or ActivationName) since i made it a singleton.

    Oh yeah. You can also set the parent object to create a Prefab. Sometimes this is needed because of like NGUI.
     

    Attached Files:

  3. Irina-Marina

    Irina-Marina

    Joined:
    Sep 16, 2012
    Posts:
    7
    Thanks, that's great tool. I'll check it out.