Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Referencing between Scenes,Accessing data only from main scene

Discussion in '2D' started by BeyondTheHorizons, Sep 28, 2020.

  1. BeyondTheHorizons

    BeyondTheHorizons

    Joined:
    Aug 25, 2020
    Posts:
    5
    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.

    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 that is in the main scene displaying stats
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Stat : MonoBehaviour {
    6.      [SerializeField] Text textMp;
    7.      [SerializeField] Text textSuspicion;
    8.      [SerializeField] Text textPersuasion;
    9.      [SerializeField] Text textHp;
    10.      static int MaxMp= 100;
    11.      static int MaxSUSPICION = 100;
    12.      static int MaxPERSUASION = 50;
    13.      static int MaxHP = 100;
    14.       int currentMp = 100;
    15.       int currentSuspicion = 0;
    16.       int currentPersuasion = 0;
    17.       int currentHp = 100;
    18.      void StartStat () {
    19.          textMp.text = currentMp.ToString ();
    20.          textSuspicion.text = currentSuspicion.ToString ();
    21.          textPersuasion.text = currentPersuasion.ToString ();
    22.          textHp.text = currentHp.ToString ();
    23.      }
    24.      private void Start () {
    25.          StartStat ();
    26.      }
    27.      public void StatManager (int Blood, int Suspicion, int Persuasion, int Hp) {
    28.          if (currentMp <= MaxMp) {
    29.              currentMp += Mp;
    30.              if (currentMp > MaxMp) currentMp = MaxMP;
    31.          }
    32.          if (currentSuspicion <= MaxSUSPICION) {
    33.              currentSuspicion += Suspicion;
    34.              if (currentSuspicion > MaxSUSPICION) currentSuspicion = MaxSUSPICION;
    35.          }
    36.          if (currentPersuasion <= MaxPERSUASION) {
    37.              currentPersuasion += Persuasion;
    38.              if (currentPersuasion > MaxPERSUASION) currentPersuasion = MaxPERSUASION;
    39.          }
    40.          if (currentHp <= MaxHP) {
    41.              currentHp += Hp;
    42.              if (currentHp > MaxHP) currentHp = MaxHP;
    43.          }
    44.          bsphText ();
    45.      }
    46.      void bsphText () {
    47.          textMp.text = currentMp.ToString ();
    48.          textSuspicion.text = currentSuspicion.ToString ();
    49.          textPersuasion.text = currentPersuasion.ToString ();
    50.          textHp.text = currentHp.ToString ();
    51.      }
    52. }
    Below is my one of the option code from the battle scene

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class dialogue1 : MonoBehaviour
    6. {
    7.     public void option1(){
    8.         var op1 = GameObject.Find("statslover").GetComponent<Stat>();
    9.         op1.StatManager(-2,2,2,-2);
    10.         SceneManager.LoadScene("Mainscene");
    11.     }
    12. }
    13.  
    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
     
    Last edited: Sep 28, 2020
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
  3. BeyondTheHorizons

    BeyondTheHorizons

    Joined:
    Aug 25, 2020
    Posts:
    5
    Yes I tried but problem is that I just need stats to be saved only not the text. When i use dont destroy on load, it carries on the text boxes as well
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    You break your Stats class apart. StatsDisplay, Stats. They can live on entirely different objects. This way you have the display be in charge of owning the texts. In contrast, Stats is a simple data holder.

    Another approach would be to copy only the data you want to transfer between scenes to a StatsTransfer ScriptableObject before you load the next scene.