Hello!I have a scene that i write down my player stats.In the next scene(basically in the next 2 scenes but it doesn’t matter) i want to buy some weapons and change the variables.The thing is i’m saving the object with “DontDestroyOnLoad” and when i go to the next scene i want to find out how can i change the variables.
First Scene:
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statsTextsToDisplayAndChanges : MonoBehaviour
{
public static statsTextsToDisplayAndChanges _INstance;
buyWeapons bw;
public Text m_LevelText;
public Text m_AttackText;
public Text m_DefendText;
public Text m_MoneyText;
public int level = 0;
public int money = 500;
public int health = 400;
public int attack = 10;
public int def = 5;
string answer;
string url = "http://alex3685.dx.am/display.php";
public int sword1 = 200;
public void Start()
{
_INstance = this;
DontDestroyOnLoad(this.gameObject);
display();
}
public void display()
{
m_LevelText.text = level.ToString();
m_AttackText.text = attack.ToString();
m_DefendText.text = def.ToString();
m_MoneyText.text = money.ToString();
}
public void onPurchase()
{
if (money >= sword1)
{
Debug.Log("YOU BOUGHT IT");
money -= sword1;
//Destroy(GameObject.Find("playerStats"));
display();
}
}
}
Second scene:
When i press the button the debug.log from the purchase function works but in the text nothing changes(from 500 coins-200 coins of the sword=300 coins)
Any ideas?
Thanks in advance
Just include the script/gameobject in every scene.
In the start of the script, check if the instance is null. If it is assign it otherwise destroy the gameobject.
Yes, you might have been duplicating the results:
Try this, as suggested:
public void Start()
{
if(_INstance == null) {
_INstance = this;
DontDestroyOnLoad(this.gameObject);
}
else if(_INstance != this) Destroy(gameObject);
display();
}
I did what you say but nothing happened!Even the debug.log didn’t work.Should i duplicate the object"playerStats" in both scenes?
When you say “I did what you say”… Who’s “you” ? 
Both xD you told me to do your script.I did something else instead and it kinda worked.i’ve added in the script
private GameObject _Manager;
public void Start()
{
_INstance = this;
DontDestroyOnLoad(this.gameObject);
display();
_Manager = GameObject.Find("playerStats"); // gia na allazei tis metavlites otan agorazei apo to shop o xristis!
Now when i go the second scene i can buy something and the coins will decrease normally but when i go to the first scene the coins stay at 500.How can i hold the variable from the second scene to the whole game ?
When you go from the second scene, back to the first, do you see two copies of the gameobject that you told to not be destroyed on load in the project hierarchy?
My bad the other thing i told you didn’t work as well.I accidentally copied the “playerstats” object in the second scene and only there it worked.I deleted it and run it but i still have the same problem.Yes i have two copies when i go to the first scene
The code that I wrote prevents the 2 copies part. So, if you do switch back n forth, you’ll want that for sure.
When you go to the second scene, are they Text variables there and if you click on them do they still properly highlight in the hierarchy? (like your Money Text, etc)
I think it should be working…
With your code it doesn’t work at all.I’m pressing the button and nothing happens.When i delete it i can see the debug.log(YOU BOUGHT IT) as i said before but in the text variables nothing changes.I don’t have any UI texts in the second scene because i think that i need only the first ones right?but i have this error
and i think it’s because i don’t have the texts gameobjects in the second scene somehow.
When i press the error i go there
Maybe i should say that i’m new to this and probably i’m doing stupid things.Sorry for that
Yes that is probably the problem. It wasn’t easy to tell if they were there from your photos. Sometimes, it doesn’t hurt to just say what’s where etc.
I made a test scene and I had just 1 text object and text that could change between scenes.
I included the text object, canvas and a parent empty object all in DontDestroyOnLoad
And when I change scenes, I keep my text object and the right # value in there, even going back n forth…
And I used my code that I wrote here, too… which you’ll see if your bugs get fixed… will be good 
If that element is common to multiple scenes, why not keep it as DontDestroyOnLoad and then you can retain the references. That’s what I would do…makes sense.
I’m sorry do you have facebook so we can talk?It’s too hard for me to understand what’s going on.It’s ok if you don’t want 
Nope, I actually don’t
Plus, I’d prefer to keep things here on the forum … it’s just easier.
When you say it’s tough to understand, does that mean the previous posts haven’t helped you at all?
I’m not sure what else I can do to explain it, other than repeating myself…
I don’t understand what exactly to do with my code.When i put this
public void Start()
{
if(_INstance == null) {
_INstance = this;
DontDestroyOnLoad(this.gameObject);
}
else if(_INstance != this) Destroy(gameObject);
display();
}
i can’t buy the weapon neither change the text variable.
When i’m having this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statsTextsToDisplayAndChanges : MonoBehaviour
{
public static statsTextsToDisplayAndChanges _INstance;
buyWeapons bw;
public Text m_LevelText;
public Text m_AttackText;
public Text m_DefendText;
public Text m_MoneyText;
public int level = 0;
public int money = 500;
public int health = 400;
public int attack = 10;
public int def = 5;
string answer;
string url = "http://alex3685.dx.am/display.php";
public int sword1 = 200;
public void Start()
{
_INstance = this;
DontDestroyOnLoad(this.gameObject);
display();
}
public void display()
{
m_LevelText.text = level.ToString();
m_AttackText.text = attack.ToString();
m_DefendText.text = def.ToString();
m_MoneyText.text = money.ToString();
}
public void onPurchase()
{
if (money >= sword1)
{
Debug.Log("YOU BOUGHT IT");
money -= sword1;
//Destroy(GameObject.Find("playerStats"));
display();
}
}
}
I can buy it but i can’t change the text variable.
You said this
What code is that?Any ideas of how i will fix the error with the text gameobjects?
Oh man… were you replacing your whole code script with just the little bit I wrote? 
That is only for the Start function… of course you have to keep the rest of the script!
yeah yeah i know
i’'ve changed only the start function! 
Well, that makes no sense how it’s not working since it’s a small change, and certainly shouldn’t affect anything other than ensuring that only 1 copy is ever active…