1
\$\begingroup\$

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?

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ It may also be worth mentioning that the Assets folder wher OP says they want to save into no longer exists in a compiled build — it's only for organizing development files — but Application.persistentDataPath gives you a location where you can save your custom files. \$\endgroup\$ – DMGregory May 13 '20 at 10:24
  • \$\begingroup\$ OK thanks, my situation is I want to save it and every time I open the game the cars' color will be the color I changed last time. Maybe I will try add a new C# component to solve it. \$\endgroup\$ – KatElectric May 13 '20 at 10:27
  • \$\begingroup\$ @KatElectric Maintaining player preferences between sessions sounds like a typical use-case for PlayerPrefs. \$\endgroup\$ – Philipp May 13 '20 at 11:34
  • \$\begingroup\$ Can always just save it as a json file as well to the persistent data path \$\endgroup\$ – Evgeny Vasilyev May 13 '20 at 17:04
  • \$\begingroup\$ Alright I solved, I used PlayerPrefs to save changed colors' RGB value and read it when a scene instantiate a car, and replace the Material 's color of cars' MeshRenderer. Thank you all. \$\endgroup\$ – KatElectric May 14 '20 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.