Referencing between Scenes,Accessing data only from main scene
Hi guys ! I am beginner game developer.
My English is poor but hopefully it makes sense
I am making 2D game where I have
Mainscene (contains "stats" code) & battlescenes (1,2,3....)
Mainscene displays current stats with text.
upon my player contacting an enemy during the main scene, it will direct you to assigned battle scene. From there, by clicking an option (made with button UI), it will increase/decrease stat as it directs you back to the main scene. The mainscene now would have updated stats from your choice earlier.
I got few things to work but I just cant reference the "stats" code in the battle scene as this script is included in the main scene.
Here is my stats code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stat : MonoBehaviour {
[SerializeField] Text textMp;
[SerializeField] Text textSuspicion;
[SerializeField] Text textPersuasion;
[SerializeField] Text textHp;
static int MaxMp= 100;
static int MaxSUSPICION = 100;
static int MaxPERSUASION = 50;
static int MaxHP = 100;
int currentMp = 100;
int currentSuspicion = 0;
int currentPersuasion = 0;
int currentHp = 100;
void StartStat () {
textMp.text = currentMp.ToString ();
textSuspicion.text = currentSuspicion.ToString ();
textPersuasion.text = currentPersuasion.ToString ();
textHp.text = currentHp.ToString ();
}
private void Start () {
StartStat ();
}
public void StatManager (int Blood, int Suspicion, int Persuasion, int Hp) {
if (currentMp <= MaxMp) {
currentMp += Mp;
if (currentMp > MaxMp) currentMp = MaxMP;
}
if (currentSuspicion <= MaxSUSPICION) {
currentSuspicion += Suspicion;
if (currentSuspicion > MaxSUSPICION) currentSuspicion = MaxSUSPICION;
}
if (currentPersuasion <= MaxPERSUASION) {
currentPersuasion += Persuasion;
if (currentPersuasion > MaxPERSUASION) currentPersuasion = MaxPERSUASION;
}
if (currentHp <= MaxHP) {
currentHp += Hp;
if (currentHp > MaxHP) currentHp = MaxHP;
}
bsphText ();
}
void bsphText () {
textMp.text = currentMp.ToString ();
textSuspicion.text = currentSuspicion.ToString ();
textPersuasion.text = currentPersuasion.ToString ();
textHp.text = currentHp.ToString ();
}
}
Below is my one of the option code from the battle scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class dialogue1 : MonoBehaviour
{
public void option1(){
var op1 = GameObject.Find("statslover").GetComponent<Stat>();
op1.StatManager(-2,2,2,-2);
SceneManager.LoadScene("Mainscene");
}
}
The script Stat is under statslover object.
I tried to use static with stats script but it gives me error. I tried dontdestroyonload method and it works but problem is that since it saves the text as well, it will override the battlescene making the buttons unable to click.
Please kindly help me do what I am thinking in my head
Thank you so much everyone