I'm designing a racing game for my graduation project. I used PrefabUtility.SaveAsPrefabAsset to save my color change on the car prefabs, I finished the codes and it work well in Unity, but when I want to build my project,the Unity give me a error:The name 'PrefabUtility' does not exist in the current context. I want to know can I not use PrefabUtility or UnityEditor to save my changed prefabs in assets directory, or are there someways to solve this problem?
1 Answers
I'm sorry, but the architecure you have built is not going to work.
All classes from the using UnityEditor
namespace are only available when you run your game from the editor. Any game which relies on these classes at runtime will fail to build for any platform. This includes any classes which create assets. So you have to find a different solution for your problem.
If it is really just about storing the color preference of the player, then you might store the RGB values of the picked color using the PlayerPrefs
class. When you instantiate the car prefab during the game, retrieve the values from PlayerPrefs
and apply them to the material of the car.
If the customization options in your game are more extensive and you want the player to be able to save multiple customization setups, then you might want to look into inventing an own file format to store that information which you then read and write using the regular C# classes for file access (If you need some examples, then Stackoverflow will have you covered). But remember that you can not write files on platforms like WebGL which do not have access to a writeable filesystem. And when you decide where to save the files, you should make use of the property Application.persistentDataPath
to get a savegame location which follows the best practices for the platform your game runs on.
If you don't even need to store the customization setups permanently and only want to communicate them from one scene to the next, then you might want to check out what other options are available for that situation.

- 119,250
- 27
- 256
- 336
Application.persistentDataPath
gives you a location where you can save your custom files. – DMGregory May 13 '20 at 10:24