- Home /
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
}
Have you tried just converting your classes to JSON?
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.
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, ...
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!
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
Are you trying to save inspector links?
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
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 :/
What is your real language, i don't understand what are you trying to do.
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.
@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
Follow this Question
Related Questions
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