- Home /
Realoding an scene doesn't load the content of a variable from another script
Hi guys, I need help solving a problem I just find while scripting for Unity3D.
This is my case:
I have 5 cards on a table. One card will add a point to the score while the other 4 will take a life away. The right card is ramdomly choose on a script attached to a different object present on the scene.
In my first card I have this script attached
public void SelectCard()
{
switch (externalVariable)
{
case 1:
UnityEngine.Debug.Log("You win a point!");
Application.LoadLevel("Mini Game 1");
break;
case 2:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Game 1");
break;
case 3:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Game 1");
break;
case 4:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Game 1");
break;
case 5:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Game 1");
break;
}
}
As you can see, if the random number is one the player will score one point if he choose the first card, but if the random number is different and the player choose the first card, he will lose a life.
The random number is stored on a int variable. To acces it, I add this to the start() section of the script attached on the first card
externalVariable= externalObject.GetComponent<MiniGame1RightCard>().rightCard;
UnityEngine.Debug.Log("The right card is card number: " + externalVariable);
So, the first card will access the random number generated by the external script attached to a different object present on the same scene, store it, and then tell me by console output which one is the right card. Then, when the player clic the first card, the script will compare the random number stored on externalVariable to check if the first card is actually the right card or not. Either way, the last step is reload the same scene to start all over again.
The very fist time the scene is loaded everything runs perfect. My problem start with the second time the scene is loaded: externalVariable is always zero. Now for the tricky part, if I put the very same code on the Update() section instead of the Start() section, for one or two seconds the externalVariable stays at zero and then change for the random number generated.
My guess is, I'm doing something wrong when I reload the scene to start again, but I don't know what.
Any ideas about why the code works fine the very first time on the Start() section and every single time on the Update() section???
UPDATE
The whole code attached to card1
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System;
using System.Threading;
public class Card1: MonoBehaviour
{
private string querySeleccionar = "SELECT Nivel FROM LevelAndDifficultyWHERE id=1;";
Text textoSilaba1;
public GameObject cardImage1;
public int level;
public int letra1;
public int silaba1;
public int externalVariable;
public GameObject objetoExterno;
public void SeleccionarSilaba()
{
switch (externalVariable)
{
case 1:
UnityEngine.Debug.Log("You win a point!");
Application.LoadLevel("Mini Juego 1");
break;
case 2:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Juego 1");
break;
case 3:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Juego 1");
break;
case 4:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Juego 1");
break;
case 5:
UnityEngine.Debug.Log("You lose a life!!!");
Application.LoadLevel("Mini Juego 1");
break;
}
}
public void OnMouseDown()
{
}
// Use this for initialization
void Start ()
{
//lot of database stuff
switch (nivel)
{
case 1:
//lots of cases
}
externalVariable= externalObject.GetComponent<MiniGame1RightCard>().rightCard;
UnityEngine.Debug.Log("The right card is card number: " + externalVariable);
}
// Update is called once per frame
void Update ()
{
}
}
The script that generate the random number
using UnityEngine;
using System.Collections;
public class MiniGame1RightCard: MonoBehaviour
{
public int rightCard;
// Use this for initialization
void Start ()
{
silabaCorrecta = UnityEngine.Random.Range(1, 5);
UnityEngine.Debug.Log("The right card is card number " + rightCard);
}
// Update is called once per frame
void Update ()
{
}
}
@$$anonymous$$dRWaylander Updated the description with the whole code.
try to use Awake() ins$$anonymous$$d of Start() in generating the random number ?
Yes try that or change the script execution order. I will add that:
silabaCorrecta = UnityEngine.Random.Range(1, 5);
will never generate 5, use (1,6) range ins$$anonymous$$d.
By the way in your code the variable you are randomizing have a different name that the on you use after but I guess you simply forgot to translate it for us.
For the record it is inclusive with the float version and exclusive with the int version: http://docs.unity3d.com/ScriptReference/Random.Range.html
If you scroll down you will see it says all that ;)
Your answer
