Way to reference between different scenes

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 (from battlescene) earlier.

Here is my stats code that is in the main scene displaying stats
Code (CSharp):

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

Code (CSharp):

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 (something like null reference). I also 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

Hey there,

help us maybe first to help you,
use code tags, it looks awfull and its hard to help :confused:

For your approach you can save your data into playerprefs and load it when you get to the new scene,
also Singletons could be your way to go,

and what do you mean by this:

Oops I have fixed the code section :slight_smile:

for the dontdestroyonload part, since it carries the displayed texts as well, my battle scene has random texts showing up…

These are the persist-across-scenes solutions I use:

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If you need then permanent forever, they already are.

If you want them only to last for the duration of a game, just destroy them beforehand (or at the end).