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

Way to reference between different scenes

Discussion in 'Scripting' started by BeyondTheHorizons, Oct 5, 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.

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

    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):

    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: Oct 5, 2020
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Hey there,

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

    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:
     
  3. BeyondTheHorizons

    BeyondTheHorizons

    Joined:
    Aug 25, 2020
    Posts:
    5
    Oops I have fixed the code section :)

    for the dontdestroyonload part, since it carries the displayed texts as well, my battle scene has random texts showing up...
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    These are the persist-across-scenes solutions I use:

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    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).