# How do I keep Text[] values alive and reuse in the script [duplicate]

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++;

return;

}

}

void Awake()
{

rootCanvas = GetComponent<GameObject>();

}

• 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? – Hamza Hasan Apr 15 '17 at 14:34
• @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. – Salar Muhammad Apr 15 '17 at 15:16
• Why are you doing this rootCanvas = GetComponent<GameObject>();? – Hamza Hasan Apr 15 '17 at 15:26
• Was just trying a way to reuse the values. Sort of like a fluke. Doesn't work though. So as we are Desi :D – Salar Muhammad Apr 15 '17 at 15:33
• Flagged this as a duplicate of: gamedev.stackexchange.com/questions/110958/… || Also take a look at this post: gamedev.stackexchange.com/questions/133872/… – John Hamilton Apr 17 '17 at 6:58

• You don't call it on the text component, but on the text component's game object: someText.gameObject.DontDestroyOnLoad() – Draco18s Apr 16 '17 at 14:01