x


How to save instantiated gameObject ??????

Hello

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

thx >>>

more ▼

asked Dec 30 '11 at 10:46 PM

firasdeep gravatar image

firasdeep
56 17 19 20

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
    }
more ▼

answered Dec 31 '11 at 02:50 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Super thx >>>>>>>> Smart tricks .....

Jan 02 '12 at 07:17 AM firasdeep

sorry i now this is old question, but i want to ask something, how about if i have many instantiated gameobject that i want to save, can this way work? if yes so i must use many variable to save the pos right? or i'm wrong?

hope you want to help me.

thanks :D

Jan 10 at 10:19 AM quantum_rez
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Dec 30 '11 at 11:44 PM

Rod Green gravatar image

Rod Green
2.9k 2 9 42

ok .. is there any smart tricks to do that >>> what i mean if there FPS game and i buy weapon and i go to play and i drop the weapon if i save the game i back to the weapon position and it destroyed...how i can save the weapon position and rotation ans the weapon is instantiated.... is there way using xml

Dec 31 '11 at 01:46 AM firasdeep

So basically you're talking about saving the state of the game. There are tricks to do this using player prefs (I.e. save limited game data to a string and write to player prefs)

Though this method is tricky there are a lot of discussions regarding this here already.

http://answers.unity3d.com/search.html?redirect=search%2Fsearch&q=Save+game+playerprefs

http://unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML

Dec 31 '11 at 01:54 AM Rod Green
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Dec 31 '11 at 12:56 AM

tatelax gravatar image

tatelax
131 11 20 23

thx >>> >>>

Dec 31 '11 at 01:46 AM firasdeep
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2090
x256
x16
x2

asked: Dec 30 '11 at 10:46 PM

Seen: 1775 times

Last Updated: Jan 10 at 10:19 AM