header

Great news! After 15 days struggling our heads to solve the prefab problem, we can confidently say that it’s been fixed! Game Creator version 0.1.8 has been submitted for review and should be live in the following days.

So, what exactly was the issue with the prefabs?

This article might get a little technical. Don’t worry, you don’t need to read it to learn how to use Game Creator. Do it only if you are curious about how it it works internally or have a similar issue.

When creating a scene game object, Unity saves this object inside a name.unity file (also known as scenes). But, when creating a prefab, this file is stored independently as a separate file inside the Assets folder.

Most of Game Creator’s core components inherit from ScriptableObject which are objects that can both live inside and outside a scene. The difference between inheriting from the default MonoBehaviour class is that these are meant to be used as components for Game Objects. ScriptableObjects are usually used only for storing data.

For example, The Actions component is a MonoBehaviour type class that contains a reference to an instance of IActionsList. This last class is responsible of keeping all the instructions that appear in the Actions component. Here’s a rough schema of this structure.

public class Actions : MonoBehaviour
{
    public IActionsList actionsList;
    // ...
}

public class IActionsList : ScriptableObject
{
    //...
}

When creating an instance of an Actions, Game Creator is smart enough to immidiatelly create an IActionsList object and add the reference to the actionsList property and store the object inside the .unity scene file.

But what happens when creating a prefab out of the Actions component? It creates an object that holds the Actions information, but not the instance of IActionsList, because there’s no .unity file to store it at, resulting on a null reference exception.

What we had to do is to detect when an Actions component changed it’s properties using the OnValidate built-in function and check whether this current object is a prefab. If it is, then, apart from storing the reference to the IActionsList instance, it also has to save the scriptable object as a child of the prefab. Here’s a super-simplified of this:

public class Actions : MonoBehaviour
{
    public IActionsList actionsList;
    //...

    void OnValidate()
    {
        IActionsList actionsList = ScriptableObject.CreateInstance<IActionsList>();
        this.actionsList = actionsList;

        // save the actionsList scriptableObject inside the
        // Project if this is a prefab.
        if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.Prefab))
        {
            // get the root of the prefab and its AssetDatabase path
            gameObject prefabRoot = PrefabUtility.FindPrefabRoot(gameObject);
            string prefabRootPath = AssetDatabase.GetAssetPath(prefabRoot);

            // Add the actionsList instance as an independent file
            // that lives inside the prefab.
            AssetDatabase.AddObjectToAsset(actionsList, prefabRoot);

            // Reimport the prefab to force Unity update the Project window.
            AssetDatabase.ImportAsset(prefabRootPath);
        }
    }
}

Obviously, the production version is much longer and contains a lot of edge cases and better management of the scriptable objects. For example, when creating a Prefab from a Scene Object, the IActionsList object should not be empty, but have a copy of the scene object’s instructions.

Another example to take into account is when duplicating objects. If an Actions component is duplicated, they both reference the same IActionsList instance, and modifying the actions will result on changing both Actions at the same time.

But once you have all these cases wrapped, it’s a pretty straight forward process. One which took us almost two weeks to figure out :-D

Cheers!

Notice that Game Creator is still in Beta but to balance things we’ve launched it 60% of its original price. Grab it now and receive future updates for free!

Download Game Creator

Martí-Joan Nogué Coll

Martí-Joan Nogué Coll - Unity Toolsmith

When he has a chance, Martí enjoys getting away with his CBF-250. He's also a fantasy bookworm.