0
\$\begingroup\$

I'm building a bowling game in which I am trying to save the number of pins fallen on every turn to a UI text box, taken as an array (public Text[] Count). I'm able to get the first score in the text box, but unable to save it while switching to other scenes.

There are two scenes; one has the ball, pins and alley, and the other scene has the score card, which is displayed in additive mode. The UI text is a child of the canvas. I've used DontDestroyOnLoad for the canvas, but it doesn't work at all. I'm actually trying to reuse these values to display the score in the series.

Below is the code

public GameObject rootCanvas;
    public Text[] Count;
    public static int counter=0;
    public static int i;
    public static int co = 0;


    MovingBall ball = new MovingBall();

    public void Start()
    {
        ball.countText = GameObject.FindWithTag("Counttext").GetComponent<Text>();


        for (i=co;i<=29;i++)
        {
            if (counter < 1)
                {
                    Count[i].text = ball.countText.text;
                }
                else if (counter == 1)
                {
                    Count[i].text = ball.countText.text;
                    Count[i+1].text = (Convert.ToInt16(Count[i-1].text) + Convert.ToInt16(Count[i].text)).ToString();
                }

            counter++;

            co++;

            DontDestroyOnLoad(gameObject.transform);

            return;

        }

    }



    void Awake()
    {

        rootCanvas = GetComponent<GameObject>();

    }
\$\endgroup\$
  • \$\begingroup\$ By loading scene additive, GameObject never destroy. I think you are finding Canvas in script and you are getting first Canvas which may be the Canvas of second scene in which is not your desired Canvas. Can you show some of your code so we can analyze it? \$\endgroup\$ – Hamza Hasan Apr 15 '17 at 14:34
  • \$\begingroup\$ @HamzaHasan there is only one canvas I've used which is independent. I've used Text[] Count which has all the textboxes as child. I've edited the code. Please refer to it. \$\endgroup\$ – Salar Muhammad Apr 15 '17 at 15:16
  • \$\begingroup\$ Why are you doing this rootCanvas = GetComponent<GameObject>();? \$\endgroup\$ – Hamza Hasan Apr 15 '17 at 15:26
  • \$\begingroup\$ Was just trying a way to reuse the values. Sort of like a fluke. Doesn't work though. So as we are Desi :D \$\endgroup\$ – Salar Muhammad Apr 15 '17 at 15:33
  • \$\begingroup\$ Flagged this as a duplicate of: gamedev.stackexchange.com/questions/110958/… || Also take a look at this post: gamedev.stackexchange.com/questions/133872/… \$\endgroup\$ – John Hamilton Apr 17 '17 at 6:58
-1
\$\begingroup\$

You need to call DontDestroyOnLoad()

That will keep your "score" tracking object alive as Unity switches scenes.

Be aware that when you go to switch back you'll now have two of these objects! You'll need to either destroy the original (before switching scenes again) or detect and destroy the duplicate (by using a singleton structure).

\$\endgroup\$
  • 1
    \$\begingroup\$ The question author says they tried that and it didn't work. \$\endgroup\$ – Philipp Apr 15 '17 at 15:29
  • \$\begingroup\$ @Philipp It would if you call it on the Text objects too \$\endgroup\$ – Draco18s Apr 15 '17 at 19:34
  • \$\begingroup\$ @Draco18s, Well I've tried on Text objects as well but it gives me error on it. \$\endgroup\$ – Salar Muhammad Apr 16 '17 at 9:29
  • \$\begingroup\$ You don't call it on the text component, but on the text component's game object: someText.gameObject.DontDestroyOnLoad() \$\endgroup\$ – Draco18s Apr 16 '17 at 14:01
  • \$\begingroup\$ Its a Text array and not a game object. \$\endgroup\$ – Salar Muhammad Apr 16 '17 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.