- Home /
How do I get a value from another script?
I want to get a value in a script from a different script. I have two scripts:
public class CargarDatos : MonoBehaviour
{
public string[] fechas;
public string dia;
IEnumerator Start()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost/Linx/Fechas.php");
yield return www.SendWebRequest();
string fechasDatos = www.downloadHandler.text;
print(fechasDatos);
fechas = fechasDatos.Split(';');
dia = GetValorDatos(fechas[0], "Dia:");
//print(dia);
}
string GetValorDatos(string datos, string index)
{
string valor = datos.Substring(datos.IndexOf(index)+index.Length);
if(valor.Contains("|"))
{
valor = valor.Remove(valor.IndexOf("|"));
}
return valor;
}
}
Here, as it is shown, a connection is established to a database and get a value (dia). This script is attached to the Main Camera, as I only want it to connect at the start of the game. Here, the function print(dia) returns the correct value, so this script works.
The problem is when I try to get the value in this script:
public class BarraExperiencia : MonoBehaviour
{
void Start()
{
print(GameObject.Find("Main Camera").GetComponent<CargarDatos>().dia);
}
}
This script is attached to a slider, and I have tried to print that to see if it works, but it doesn't print anything. What have I done wrong? Thanks.
Answer by Hellium · Nov 14, 2019 at 07:14 AM
The problem is that BarraExperiencia
tries to get the value of dia
in the Start
function while the coroutine of CargarDatos
has not finished yet.
You can use events to warn BarraExperiencia
the coroutine has finished
public event System.Action<string> DayRetrieved;
IEnumerator Start()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost/Linx/Fechas.php");
yield return www.SendWebRequest();
string fechasDatos = www.downloadHandler.text;
print(fechasDatos);
fechas = fechasDatos.Split(';');
dia = GetValorDatos(fechas[0], "Dia:");
//print(dia);
DayRetrieved?.Invoke(dia);
}
public class BarraExperiencia : MonoBehaviour
{
void Start()
{
CargarDatos cargarDatos = GameObject.Find("Main Camera").GetComponent<CargarDatos>();
cargarDatos.DayRetrieved += OnDayRetrieved
}
void OnDayRetrieved(string dia)
{
print(dia);
}
}
Check the comments, next is to pass more data than just the string.
Yes, it works like that, but I don't want to print the value of dia, it was only to check if it works. I want to get the value in the BarraExperiencia script and keep it in a variable, ins$$anonymous$$d of printing it.
Answer by Captain_Pineapple · Nov 13, 2019 at 07:27 PM
Do you get a nullreference error?
either way it'd be way safer to use FindGameObjectOfType<CargarDatos>().dia
to find one instance of a script and access the variable.
Even better here would be to make dia static so you can access the dia value by CargarDatos.dia
I don't get any error, the problem is that it doesn't print anything.
did you make sure the BarraExperiencia SCript is attached to a transform in your scene and is active?
The BarraExperiencia script is attached to a slider and it works because I'm using other method with it which I have not shown in the code (it's not relevant to this matter).
Answer by Gilbert8614 · Nov 15, 2019 at 07:43 AM
The BarraExperiencia script is attached to a slider and it works because I'm using other method with it which I have not shown in the code (it's not relevant to this matter).
Your answer
