Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by raffaelebasile · May 05, 2018 at 07:59 AM · scripting problemscripting beginnerplayerprefsscriptable objectdata storage

Save different game object's datas using the same script?

Hi everybody, I've a little problem. I'm making a simple application in witch user do maintenance to electrical system of a building. I created a script that allows you to inser the current date in whcich you are doing maintenance and automatically appear the maintenance deadline. It works great, but if add the same script to 2 different game object my code don't save either datas. How can I solve this problem? thank you a lot.

Here there is the script:

using UnityEngine; using UnityEngine.UI; using System.Collections; using System; public class Clock : MonoBehaviour { #region Declarations public static float unscaledTime; //refer to this value when determining timed-event checks (measures playtime in seconds since the start of the game). public static float time; //refer to this value "Time.timeElapsed" when determining in-game timed-event checks such as weather (measures time in seconds since the start of the game inclusive of timescale effects) [SerializeField] private Text clockText;

 [SerializeField]
 private Text dateText;
 [SerializeField]
 private float clockSpeed = 1f;
 DateTime currentTime;
 DateTime deadlineTime;
 
 DateTime maintenanceTime;
 public int howMuchTime;

 #endregion

 public Text dateMaintenance;
 public Text maintenanceDeadline;
 private int dateConverter;
 private int dateConverterDAY;
 private int dateConverterMONTH;
 private int dateConverterYEAR;
 private int currentdate;
 private int currentdateDAY;
 private int currentdateMONTH;
 private int currentdateYEAR;


 public GameObject exclamationPoint; 
 

 void Start() {
     currentTime = System.DateTime.Now;

     #region date converter
     currentdateDAY= int.Parse(currentTime.ToString("dd"));
     currentdateMONTH = int.Parse(currentTime.ToString("MM"));
     currentdateYEAR = int.Parse(currentTime.ToString("yyyy"));
     currentdate = currentdateDAY + (currentdateMONTH * 30) + (currentdateYEAR * 365);
     Debug.Log(currentdate);
     #endregion

     exclamationPoint.SetActive(false);
    dateMaintenance.text = PlayerPrefs.GetString("dateMaintenance", currentTime.ToString("dd/MM/yyyy")).ToString();
     maintenanceDeadline.text = PlayerPrefs.GetString("maintenanceDeadline", deadlineTime.ToString("dd/MM/yyyy")).ToString();
     dateConverter = PlayerPrefs.GetInt("dateConverter",dateConverter);

     Debug.Log(dateConverter);
     

     if ((dateConverter - currentdate) < 7){ 
     
         Debug.Log("Meno di 7 giorni! Fare manutenzione a breve!!");
         exclamationPoint.SetActive(true);


     }
     else {

Debug.Log("più di 7 giorni! C'è ancora tempo"); exclamationPoint.SetActive(false); }

 }


 #region Clock
 void Awake()
 {
     
     Time.timeScale = 1.0f;
     InvokeRepeating("Lapse", 0, clockSpeed * Time.timeScale);
 }
 void Lapse()
 {
     currentTime = System.DateTime.Now;
     
     clockText.text = currentTime.ToString("hh:mm:ss");
     dateText.text = currentTime.ToString("dd/MM/yyyy"); //Specifies the name of the day, its date, the name of the month, and the year);


 }
 public void NormalSpeed()
 {
     Time.timeScale = 1.0f;
 }
 void MinuteSpeed()
 {
     Time.timeScale = 60.0f;
 }
 void HourSpeed()
 {
     Time.timeScale = 600.0f;
 }

 public void maintenanceCommand() {

     dateMaintenance.text = currentTime.ToString("dd/MM/yyyy");
     deadlineTime = currentTime.AddYears(howMuchTime);

     #region date converter
     dateConverterDAY =  int.Parse(deadlineTime.ToString("dd"));
     dateConverterMONTH = int.Parse(deadlineTime.ToString("MM"));
     dateConverterYEAR = int.Parse(deadlineTime.ToString("yyyy"));
     dateConverter = dateConverterDAY + (dateConverterMONTH * 30) + (dateConverterYEAR * 365);
     Debug.Log(dateConverter);
     #endregion

     Debug.Log(currentTime);
     maintenanceDeadline.text = deadlineTime.ToString("dd/MM/yyyy");

     PlayerPrefs.SetString("dateMaintenance", currentTime.ToString("dd/MM/yyyy"));
     PlayerPrefs.SetString("maintenanceDeadline", deadlineTime.ToString("dd/MM/yyyy"));
     PlayerPrefs.SetInt("dateConverter", dateConverter);
    
     exclamationPoint.SetActive(false);
     Debug.Log("Manutenzione avvenuta!!");

 }

 public void Reset() {
     PlayerPrefs.DeleteAll();
     dateMaintenance.text = "00/00/0000";
     maintenanceDeadline.text = "00/00/0000";
 }


 #endregion

}

Comment
Add comment · Show 1
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 Sabre-Runner · May 05, 2018 at 08:50 AM 0
Share

Have you tried just converting your classes to JSON?

3 Replies

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

Answer by Sabre-Runner · May 07, 2018 at 07:42 AM

It seems to me that you have two entities with the same script and you're using a single PlayerPrefs key for both. You should differentiate the objects -- like by their name -- and have the key dependent on that.

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 raffaelebasile · May 07, 2018 at 08:31 AM 0
Share

How can I do this?

avatar image Sabre-Runner · May 07, 2018 at 08:54 AM 0
Share

Call your objects, in the scene, by different names and use that GameObject.name as part of the key you use to save things. Like PlayerPrefs.SetSomething("SAVE_$$anonymous$$EY_" + GameObject.name, ...

avatar image
0

Answer by SharkoFR · May 05, 2018 at 08:53 AM

Hi! you could use https://assetstore.unity.com/packages/tools/input-management/saveme-pro-gameobject-saver-107916 to save all your GameObjects!

Comment
Add comment · Show 5 · 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 raffaelebasile · May 05, 2018 at 09:03 AM 0
Share

it's not what I'm looking for... I have to save string and integer variables which are linked to lot of game object with the same script

avatar image Sabre-Runner raffaelebasile · May 05, 2018 at 09:09 AM 0
Share

Are you trying to save inspector links?

avatar image raffaelebasile Sabre-Runner · May 05, 2018 at 09:32 AM 0
Share

I made some screenshot hoping to be clearer.

in the firsth image you can see that I've 2 element. to this element I linked the Saved Clock UI button. Clicking on it my script save the date next to the text called "maintenance done" (for deadline I added 1 year using the function currenttime.addyear) When I close and open again the application you can see that the program saved the same date for each element. I think that the problem is because I don't have a list...but I've started program$$anonymous$$g since 1 month, so I'm a beginner and I don't know how to solve this problem.

I hope I have been clearer. Thank you :)

So,

![alt text][2] [2]: /storage/temp/116247-3.png

2.png (80.3 kB)
3.png (80.6 kB)
Show more comments
avatar image
0

Answer by raffaelebasile · May 05, 2018 at 11:06 AM

@Sabre-Runner I try to be clearer: This is the actual situation: I've a cube on right and a cube on left. Both cube have as child the 2 date called " datemaintenance" and " scadenzaMannutenzione". The other one with "(1)" are simply texts.

Now, I signed how I've linked text to the script. I wrote the script at the top of this post.

His 2 cube are identical. The problem is the following: When i click on the button called "Saved Clock" on the right of the scene the date under it change to the acutal date, and the date on the left doesn't.

in the script I added the feature that allows to save the date and to show it when you open again the application, But when I open again the app, the date on left is changed and it's equal to the date on right.

I think that the problem could be that I don't have set some feature that allow me to save more data then one only. But I don't know how to do this.

Hope to be clear, sorry for my bad englih :/

alt text


project.png (358.8 kB)
Comment
Add comment · Show 3 · 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 SharkoFR · May 05, 2018 at 11:46 AM 0
Share

What is your real language, i don't understand what are you trying to do.

avatar image Sabre-Runner · May 06, 2018 at 03:57 AM 0
Share

How are you saving? PlayerPrefs? $$anonymous$$aybe you're saving on the same key because it's the same script doing the saving? So what is actually saved is the last thing.

avatar image raffaelebasile Sabre-Runner · May 06, 2018 at 04:04 PM 0
Share

@Sabre-Runner yes, actually I'm saving with playerprefabs. I think I will not be able to use it anymore. This that I showed you is an exaple that I'm using to do some tests... in the real project that I'm working on has about 300 items of which I have to save the individual dates when I do maintenance and I can't create 300 playerprefabs. Looking around I've hunderstand that a good way to save lots of data is using Json files, I tried but I can't managed... Do you have some advaise?

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

152 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 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 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 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 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 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 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 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 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 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 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

I have a rotation script that is applied to two different objects. If I rotate one it also rotates the other and I do not want that. 2 Answers

Coins not updating after making shop transactions 0 Answers

[C#] How to make this code toggleable 3 Answers

How to lock an int as a playerPref? 1 Answer

Player Prefs Dosen't work on android 0 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