"Prefab" a gameobject to reference in later scenes?

I’m not sure how to word this, please forgive me if this is a question thousands have asked before but being new to C#, Unity, and the terminology, I’m not sure how to word it, or what kind of words are describing what I’m looking for, and therefore I’m not sure if the research and searching I’ve done has actually led me to an answer.

So basically, you create a 2D character with my in-game character creation screen. From there, Unity is able to save an instance of the character you create, and your character appears whenever the player talks using Fungus.

I have no idea where to start with this. Do I need to set the character to pre-fab and then reference the created pre-fab later? Is this even possible to do in Fungus? Or is there a different method? Similar to how Dream Daddy did it. They used Unity and Fungus, with a character selection screen, and the “Daddy” you create at the beginning of the game is frequently referenced whenever your character speaks.

To be clear, the character creation menu is complete, I’m looking to implement how Unity will handle “saving” the character that the player has made, and then how Unity/Fungus can handle referencing this player character in different scenes and later in the game.

I’d appreciate an arrow in the right direction, so if you have one to offer, that would be great. There seems to be very little in the way of “character creation” tutorials that go over this instance of the coding/developing, everything I’ve come across is developing a way to script the sprites and make things interchangeable, rather than referencing what would be the already created player character later in the game.

Prefabs can’t be created at runtime, so in this case prefabs are not the tool you’ll want to use. Prefabs are assets, that is, something that you as the game creator make; they don’t get changed or created by players in the game.

There are a few different ways to accomplish what you’re going for.

  1. Just don’t destroy the character. Call DontDestroyOnLoad and have your character persist across scenes. This is by far the easiest approach if the character doesn’t need to persist across game loads.
  2. Save data about how you created the character, and rebuild the character the same way when needed. This could take a number of forms, from a class you’ve defined with specific variables, to something more generic like a list of prefabs and places in the hierarchy.

If you want the player to be able to load this character after they have quit the app, then something more like #2 is what you want to do. You’ll want to use something like JSON (Maybe via Newtonsoft’s Json.NET) to save this data to a file (probably a file within Application.persistentDataPath), and load it back. When loading, you’ll use logic that replicates the abilities of the character creation scene, but instead of getting user input it reads the information from this data.

Hopefully that’s enough to give you a place to start and some terms to google; if you have more specific questions as you work through the process feel free to ask.

2 Likes