Help with "Name does not exist in current context" error.

Hey all, I’m using PrefabUtility to store and pull player GameObjects from my resources and have been having some issues when building the game because of it. Here’s the code I’ve written:

using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class RunDataManager : MonoBehaviour
{
    public static void SaveDataFromLevel()
    {
        PrefabUtility.SaveAsPrefabAsset(GameObject.FindGameObjectWithTag("Player"), "Assets/Resources/RunData/PlayerObject.prefab");
        PrefabUtility.SaveAsPrefabAsset(GameObject.FindGameObjectWithTag("DroneHolder"), "Assets/Resources/RunData/DroneHolderObject.prefab");
    }
    public static void LoadData(out GameObject playerObject, out GameObject droneHolder, out RunData runData)
    {
        playerObject = (GameObject)Resources.Load("RunData/PlayerObject");
        droneHolder = (GameObject)Resources.Load("RunData/DroneHolderObject");
        runData = Resources.Load("RunData/RunData").GetComponent<RunData>();
    }
}

And here’s an image of the errors I’m getting when I build the game:

The game launches and the editor perfectly fine, and works exactly how I want it to. This error only pops up when building. Any thoughts on how this could be fixed? I tried searching but couldn’t find an exact solution.

Thank you!

PrefabUtility is only available in the editor, not in a build. Generally any namespace with ‘Editor’ cannot be included in a build.

You either need to make this an editor-only script in some way, or just surround the affected code with #if UNITY_EDITOR pre-processor directives.

Also this should just be a static class and not a monobehaviour. No reason for it to be a monobehaviour class.

1 Like

Alright, noted.

What alternative method would you recommend for carrying GameObjects over between scenes?

I mean you can’t save assets in a build at all either. Once built, a game’s assets cannot be changed (remote content delivery notwithstanding). Your Assets folder and similar don’t exist in a build at all either.

If you want to persist something between scene loads, just use DontDestroyOnLoad: Unity - Scripting API: Object.DontDestroyOnLoad

1 Like