-1

Unity (C#): How do I call a List from a different Scene? I get nullReferenceException bc in the current scene, it has no variables set to it. But in the previous Scene it had the correct values and variables that I wanted. (note: the 2 scenes are different but I want to use the score as a shared value between Scenes).

Otherwise, do you have an alternative to retrieving the "frameTexts[9].text" position?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class ScoreDisplay : MonoBehaviour
{

    public Text[] rollTexts, frameTexts;
    static string output;

    public void FillRolls(List<int> rolls)
    {
        string scoreString = FormatRolls(rolls);
        for (int i = 0; i < scoreString.Length; i++)
        {
            rollTexts[i].text = scoreString[i].ToString();

        }
    }

    public void Fillframes(List<int> frames)
    {
        for (int i = 0; i < frames.Count; i++)
        {
            frameTexts[i].text = frames[i].ToString();
            Debug.Log(frameTexts[i].text + " frames");
        }
    }

    public static string FormatRolls(List<int> rolls)
    {
      //  ScoreDisplay scoreDisplay = new ScoreDisplay();
        output = "";

        for (int i = 0; i < rolls.Count; i++)
        {
            int box = output.Length + 1;                            // Score box 1 to 21 

            if (rolls[i] == 0)
            {                                   // Always enter 0 as -
                output += "-";
            }
            else if ((box % 2 == 0 || box == 21) && rolls[i - 1] + rolls[i] == 10)
            {   // SPARE anywhere
                output += "/";
            }
            else if (box >= 19 && rolls[i] == 10)
            {               // STRIKE in frame 10
                output += "X";
            }
            else if (rolls[i] == 10)
            {                           // STRIKE in frame 1-9
                output += "X ";
            }
            else
            {
                output += rolls[i].ToString();                      // Normal 1-9 bowl
            }

        }
        return output;
    }
    public string DisplayFinalScore()
    {
        string FinalScore = "Congrats on scoring: " + frameTexts[9].text;
        return FinalScore;
    }

} 
BenSmith
  • 429
  • 1
  • 6
  • 14
  • 2
    let the List be static and be inside a class that doesn't inherit from monobehaviour – Daahrien Jun 02 '17 at 04:58
  • 2
    Possible duplicate of [Unity - pass data between scenes](https://stackoverflow.com/questions/32306704/unity-pass-data-between-scenes) – Serlite Jun 02 '17 at 05:04

3 Answers3

2

Use DontDestroyOnLoad in the script for the object that contains the variables you want to retain from one scene to another.

  • I used DontDestroyOnLoad as "DontDestroyOnLoad(frameTexts[i]);". However, I get the warning "DontDesotryOnLoad only works on root GameObjects or components on root GameObject". And the NullReferenceException occurs as before. Any clue why it's not working? – BenSmith Jun 02 '17 at 05:33
  • You can't use DontDestroyOnLoad on variables. Attach your script to a empty gameobject and call this DontDestroyOnLoad(transform.gameObject); transform.gameObject is the object you will not destroy. – rohankad Jun 02 '17 at 05:37
  • Please note you can simply use `DontDestroyOnLoad(gameObject)` or `DontDestroyOnLoad(this)` on a **MonoBehaviour** class (not even sure why Unity example is `DontDestroyOnLoad(transform.gameObject)`: this creates useless calls). Also remember your object holding the `DontDestroyOnLoad` script has to be a root object (not a child of another object): if the parent doesn't have a `DontDestroyOnLoad` script, its child will get deleted. – Kardux Jun 02 '17 at 08:06
1

I think your request depend on what you wanna do in your project. But i can recommend some few solutions:

  • Singleton (unitygeek - gist)
  • Save your scores in external file
  • PlayerPref

If you wanna keep the scores entire the whole game, i think singleton will be nice !!

0

You can use DontDestroyOnLoad(transform.gameObject);so that the gameobject will be active in your next scene or use PlayerPrefs to save the data and load in the next scene. Both shall work!

rohankad
  • 79
  • 7