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.