How to save instantiated gameObject ??????

Hello

is there are way to save instantiated GameObject in Runtime>>>> Like saveing int,float and string >>>>>>>

thx >>>

You can use the PlayerPrefs to save an object’s position and rotation in a string, and when the level is reloaded, re-instantiate or move the object to the saved position/rotation. This is somewhat tricky to be done because you must decode the string into the original values.

To save the “Weapon2” object’s position and rotation, you can do something like this:

    var s = transform.position.ToString()+transform.rotation.ToString();
    PlayerPrefs.SetString("Weapon2", s);

This saves a string like this: “(123, 2, 209)(0.5, 0.3, 0.3, -0.7)”, associated to the key “Weapon2”.

When loading the level, try to GetString with this key; if it’s found, decode the string and get the position and rotation from it and move or create the weapon:

    var sWpn2 = PlayerPrefs.GetString("Weapon2","notfound");
    if (sWpn2 != "notfound"){ // if Weapon2 key found...
        var delims: char[] = "(),".ToCharArray(); // define delimiters
        // split the values in an array
        var values: String[] = sWpn2.Split(delims);
        var pos: Vector3;
        var rot: Quaternion;
        pos.x = float.Parse(values[1]);
        pos.y = float.Parse(values[2]);
        pos.z = float.Parse(values[3]);
        rot.x = float.Parse(values[5]);
        rot.y = float.Parse(values[6]);
        rot.z = float.Parse(values[7]);
        rot.w = float.Parse(values[8]);
      // create the weapon using this position/rotation...
        Instantiate(wpnPrefab, pos, rot);
      // or move the existing weapon there:
        weapon.transform.position = pos;
        weapon.transform.rotation = rot;
    }

Really without manually saving the data (to file - as any properties will be reset/initialized) you can’t.

What are you trying to do? Can you set the GameObject up with ‘ExecuteInEditMode’ without playing?

If you mean saving for the next scene you could use

http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html

This is older, but I think I have a slightly better approach - I took a similar approach to aldonaletto, but there is a way to simplify it:

You can store (Object)s just fine using the serializer, so what I do is convert everything to object when it is saved, then on load I reconvert it, it works perfectly. You can use this for dictionaries, arrays, etc. as well.

The following you should consider pseudo code, my save and load data route through special classes that encrypt and decrypt stuff so you can’t just copy and paste the following, but it gives you an idea of what I mean.

Example:

//Note, for the array, I happen to know the size of the array ahead of time, but it's
//trivial if you don't, you can store the array size in an int, pull it out before you get
//the array and loop through with the pulled out value.

//Variables in class called "SUPlayerResources":
private bool m_bTimersStarted;
private float m_fTimerUpdate;
private float[]	m_fPlayerGoldPerTimer; 

OnSaveData() {
 StoreData( "SUPlayerResources", "m_bTimersStarted", (object)m_bTimersStarted);
 StoreData( "SUPlayerResources", "m_fTimerUpdate", (object)m_fTimerUpdate);
 for (int i=0; i <= SUSettings.m_iTotalPlayers; ++i) {
   StoreData( "SUPlayerResources", "m_fPlayerGoldPerTimer" + i, (object)m_fPlayerGoldPerTimer*);*

}
}

OnLoadData() {
m_bTimersStarted = (bool) GetData( “SUPlayerResources”, “m_bTimersStarted” );
m_fTimerUpdate = (float) GetData( “SUPlayerResources”, “m_fTimerUpdate” );
//loading the array:
for (int i=0; i <= SUSettings.m_iTotalPlayers; ++i) {
m_fPlayerGoldPerTimer = (float) GetData( “SUPlayerResources”, “m_fPlayerGoldPerTimer” + i );
}

}

Read this: http://unitygems.com/saving-data-1-remember-me/
And this: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

There is a “Saving to a file” section which may help you because storing data with playerPrefs is not secure unless you encrypt it. However, Unity offers saving byte files to a persistent folder path (Application.persistentDataPath). What’s good about this is that it works with Android and iOS with their respective security handled when saving locally.

The best solution I have ever seen is using http://whydoidoit.com/unityserializer

you can check this post
http://forum.unity3d.com/threads/save-load-a-gameobject-to-an-external-file.179428/
and this

Hi.
I made a long script with advanced load and save system.
Everything is about File stream. Every position, rotation and scale (basic datas of every object) are saved to txt file as a new Line.

You can save and load your instantiated objects. But first, if you want to load different meshes except Cube, you must create Resources folder in your assets project and duplicate meshes which will be loaded in a future.

Also you can set which objects will not be saved or loaded. You can edit script also, it is free and my without copy…
Download here: [61584-save-load-system-v2.zip|61584]