That pesky Renaming bug (Realm Rush)

So the way things are now, in Realm Rush, every time you go to edit Tile in the prefab mode, the CoordinateLabeler script goes and tries to rename the root of the prefab to 0,0, and you get a message saying that the root of the prefab must match the filename. We’ve been ignoring the error, but there is a way to make it go away.
The issue is that we need to know not just whether or not we’re playing the game, but whether or not we’re currently editing the prefab. We cover the first part in the Update() method

void Update()
    {
        if (!Application.isPlaying)
        {
            DisplayCoordinates();
            UpdateObjectName();
        }
    }

The if(!Application.isPlaying) prevents the script from running while we’re playing our game. But how do we tell if we’re in Prefab mode?
Fortunately, there’s a class called PrefabUtility. It’s not very well documented, and the results from most of it’s functions don’t always make sense, but there is a static routine you can call to determine if your gameobject is a prefab or an instance in the scene:

PrefabStageUtility.GetPrefabStage(gameObject);

This will return null if the gameObject is in the scene, meaning we can invert this to determine if it’s not in the scene. Since we only want to change scene instances, this method will serve our purpose.

void Update()
    {
        if (!Application.isPlaying && PrefabStageUtility.GetPrefabStage(gameObject) == null)
        {
            DisplayCoordinates();
            UpdateObjectName();
        }
    }

Now the script will run in edit mode, when the GameObject is in the scene, and will not try to rename the root of your prefab when it is in Prefab mode.

10 Likes

Thank you for mentioning this @Brian_Trotter, this is a fantastic tip!

The last time I checked this feature was still in the “experimental” stage, so could still be liable to change.
However, I would definitely recommend adding this additional line of code (or whatever it morphs into later), as it can stop a lot of headaches and really helps to speed up your workflow.

For anyone having trouble getting this code to work in their project, you may also need to include the following line to the top of your script:
using UnityEditor.Experimental.SceneManagement;

8 Likes

Yep, it’s still in the experimental stage.
Silly me, I use Jetbrains Resharper, so I conveniently forget to think about the namespaces since it adds them automatically. That also reminds me, it’s probably a good idea to surround the using clause and the Update method with #if UNITY_EDITOR or the game won’t build.

3 Likes

You’re absolutely right, using anything from UnityEditor will cause problems when it comes to building the project.
I’ve mentioned this issue in a later lecture and provided a simple fix that will get the project to build.

4 Likes

Hello Gary and Brian and Everyone reading this
just a quick update here about the namespace.
It is no longer using UnityEditor.Experimental.SceneManagement;

correct and up to date namespace is:
using UnityEditor.SceneManagement;

1 Like

Good catch! It’s nice to see they’ve moved this out of experimental status.

I’ll be working on Realm Rush in the near future so I want to mark this and save it for later.

Thank you! @Brian_Trotter

Privacy & Terms