Search Unity

Create Prefab at Runtime = Possible?

Discussion in 'Scripting' started by Raison, Jul 8, 2013.

  1. Raison

    Raison

    Joined:
    Apr 21, 2013
    Posts:
    20
    Hey ,

    I just wanted to ask if its possible to create a Prefab at runtime or change a prefab at runtime.
    E.g. I want to load a Character Prefab change things at runtime and want to save them.Is this
    possible or is there another way of doing it?

    Hope you guys can help me or give me a hint.

    Raison
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    No, that is not possible.
     
  3. Raison

    Raison

    Joined:
    Apr 21, 2013
    Posts:
    20
    So there is no way around?Is there only the way to use PlayerPrefs to change Variables and save them?
     
    Last edited: Jul 8, 2013
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You can save anything you want with PlayerPrefs. What I meant was that you can't save/change a prefab at runtime.
     
  5. Raison

    Raison

    Joined:
    Apr 21, 2013
    Posts:
    20
    So actually what i want to get is a simple way to save all things behave to the character. Hope anyone know a easy way because
    calling 100x PlayerPrefs isnt that what i want.

    Hope anyone knows a answer.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    A Prefab is just a copy of a GameObject that's not in a scene. Depending on what you want, you could simply keep an inactive copy of your template GameObject hidden in your scene, change that as you wish, and use it rather than a "prefab" as the argument when you call Instantiate.

    Another alternative is to keep the stuff you want to be able to change in a separate data object, and apply the data to your object upon instantiation. The data object can be stored and loaded as per any other .NET object - remember that you have full access to serialization, file IO, and so on and so forth (except for when using the web player, where file IO is restricted - look to use network based methods there instead).

    Also, you're right, calling hundreds of PlayerPrefs isn't a good idea. Just looking at the name gives a pretty good indicator that the system is intended to store preferences, not bulk data.
     
    Last edited: Jul 9, 2013
  7. FredericRP

    FredericRP

    Joined:
    Jan 11, 2010
    Posts:
    79
    You should look into ScriptableObject to store your data, but you still have to store and load it like angrypenguin said via IO stuff or http, depending on your target.
     
  8. Raison

    Raison

    Joined:
    Apr 21, 2013
    Posts:
    20
    Thanks for the help.