Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by firasdeep · Dec 30, 2011 at 10:46 PM · gameobjectxmlinstantiated

How to save instantiated gameObject ??????

Hello

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

thx >>>

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Dec 31, 2011 at 02:50 AM

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;
    }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image firasdeep · Jan 02, 2012 at 07:17 AM 0
Share

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

avatar image quantum_rez · Jan 10, 2013 at 10:19 AM 1
Share

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

avatar image valtteri_m · Apr 14, 2016 at 12:58 PM 0
Share

What is the "rot.w" ?

avatar image Cherno valtteri_m · Apr 14, 2016 at 04:46 PM 0
Share

(I assume you realize this thread is three years old)

rot.w is the w axis of the Quaternion variable rot.

avatar image
1

Answer by Rod-Green · Dec 30, 2011 at 11:44 PM

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image firasdeep · Dec 31, 2011 at 01:46 AM 0
Share

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

avatar image Rod-Green · Dec 31, 2011 at 01:54 AM 0
Share

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_X$$anonymous$$L

avatar image
0

Answer by tatelax · Dec 31, 2011 at 12:56 AM

If you mean saving for the next scene you could use

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image firasdeep · Dec 31, 2011 at 01:46 AM 0
Share

thx >>> >>>

avatar image
0

Answer by SinisterRainbow · Dec 19, 2013 at 06:58 PM

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[i]);
  }
 }
 
 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[i] = (float) GetData( "SUPlayerResources", "m_fPlayerGoldPerTimer" + i );
  }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by pavlito · May 27, 2014 at 01:51 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Return a Exist GameObject 0 Answers

Can gameobject be exported as unity3d form? 0 Answers

Trouble with save file 1 Answer

A node in a childnode? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges