I'm trying to make a script that reads the variables of another script and stores them in itself, but I can´t get the data stored and I don't know how else to do it, please help.
Unity 2019.1
public class contenedor2 : MonoBehaviour
{
public string elemento1;
public void finalizado()
{
contenedor elementos = GetComponent<contenedor>();
elemento1 = elementos.contenedor1;
}
But the script does not read the variables ( contenedor is a script associated with an element of another scene which contains the variables that I want to read)
You cannot. If a scene is not loaded, then it effectively does not exist until it is loaded.
What you might try instead is putting the script with the data into a prefab that's used by all scenes that need it. If it's script-only variables, you could also try a static class, but then you need to manage the initialization and shutdown yourself.
With your current code structure, Your contendor class can have the variable as static.
public class contenedor : MonoBehaviour
{
public static string elemento1;
}
public class contenedor2 : MonoBehaviour
{
public string elemento1;
public void finalizado()
{
elemento1 = contenedor.elemento1;
}
PS: Code can be improved, Let me know if it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.