Ver Mensaje Individual
  #6 (permalink)  
Antiguo 12/12/2014, 18:50
Dempo87
 
Fecha de Ingreso: octubre-2013
Ubicación: Murcia
Mensajes: 54
Antigüedad: 11 años, 4 meses
Puntos: 0
Respuesta: ¿Como acceder desde un script a una variable instanciada en otro script?

Hola de nuevo, tengo otra duda, jeje en realidad varias

Desde el script "DatosPersonaje" accedo a las variables de la clase "Datos", algunas variables las puedo coger, otras no, y no paro de mirar el cogido y no veo el por que, no se si despues del "datosPersonaje.edad" tengo que poner ToString(), o que deberia hacer.

CLASE

Código C#:
Ver original
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Datos {
  5.  
  6.     //Variables
  7.     public string nombre = "";
  8.     public int edad;
  9.     public float peso = 0;
  10.     public float altura = 0;
  11.     public string nacionalidad = "";
  12.     public string equipo = "";
  13.     public string[] demarcacion = new string[] {"Portero","Defensa Central","Defensa Lateral","Mediocentro Defensivo","Medio Lateral","Mediocentro","Mediocentro Ofensivo","Extremo","Delantero centro"};
  14.     public int indiceDemarcacion = 0;
  15.     public int numeroJugador;
  16.  
  17.     public Vector2 posicionDatos = new Vector2(50, 50);
  18.     public int subirNum = 1;
  19.  
  20. }

SCRIPT

Código C#:
Ver original
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DatosPersonaje : MonoBehaviour {
  5.  
  6.     //ESCENA DATOS PERSONAJE
  7.     public Datos datosPersonaje;
  8.  
  9.     //GUISkin
  10.     public GUISkin myGUISkin;
  11.     public GUISkin textoPorDefecto;
  12.     public GUISkin flechaDer;
  13.     public GUISkin flechaIzq;
  14.  
  15.     //GUIStyle
  16.     public GUIStyle textField;
  17.  
  18.     void Start(){
  19.         datosPersonaje = new Datos();
  20.  
  21.     }
  22.  
  23.     void Update(){
  24.  
  25.     }
  26.  
  27.     void OnGUI(){
  28.  
  29.         //ESCENA DATOS PERSONAJE
  30.         //Modulo central trasparente
  31.         GUILayout.BeginArea(new Rect(datosPersonaje.posicionDatos.x, datosPersonaje.posicionDatos.y, Screen.width * 0.9f, Screen.height * 0.9f));
  32.  
  33.         GUI.skin = myGUISkin;
  34.  
  35.         GUI.Box(new Rect(Screen.width / 2 - 175, 100, 350, 350), "DATOS DEL PERSONAJE");
  36.  
  37.         GUI.skin = textoPorDefecto;
  38.  
  39.         GUI.Label(new Rect(Screen.width / 2 - 150, 130, 100, 25), "Nombre");
  40.         datosPersonaje.nombre = GUI.TextField (new Rect(Screen.width / 2 - 60, 128, 200, 25), datosPersonaje.nombre, 20);
  41.  
  42.         GUI.Label(new Rect(Screen.width / 2 - 150, 160, 100, 25), "Edad");
  43.         //datosPersonaje.edad = GUI.TextField (new Rect(Screen.width / 2 - 60, 158, 200, 25), datosPersonaje.edad, 2);
  44.  
  45.         GUI.Label(new Rect(Screen.width / 2 - 150, 190, 100, 25), "Peso");
  46.         //datosPersonaje.peso = GUI.TextField(new Rect(Screen.width / 2 - 60, 188, 200, 25), datosPersonaje.peso, 2);
  47.  
  48.         GUI.Label(new Rect(Screen.width / 2 - 150, 220, 100, 25), "Altura");
  49.         //datosPersonaje.altura = GUI.TextField(new Rect(Screen.width / 2 - 60, 218, 200, 25), datosPersonaje.altura);
  50.  
  51.         GUI.Label(new Rect(Screen.width / 2 - 150, 250, 100, 25), "Nacionalidad");
  52.         datosPersonaje.nacionalidad = GUI.TextField(new Rect(Screen.width / 2 - 60, 248, 200, 25), datosPersonaje.nacionalidad, 20);
  53.  
  54.         GUI.Label(new Rect(Screen.width / 2 - 150, 280, 100, 25), "Equipo");
  55.         datosPersonaje.equipo = GUI.TextField(new Rect(Screen.width / 2 - 60, 278, 200, 25), datosPersonaje.equipo, 20);
  56.  
  57.         GUI.Label(new Rect(Screen.width / 2 - 150, 310, 100, 25), "Demarcacion");
  58.         GUI.TextField(new Rect(Screen.width / 2 - 60, 308, 200, 25),"");
  59.         GUI.Label(new Rect(Screen.width / 2 - 47, 310, 200, 25), datosPersonaje.demarcacion[datosPersonaje.indiceDemarcacion]);
  60.  
  61.         GUI.skin = flechaIzq;
  62.         if (GUI.Button(new Rect(Screen.width / 2 - 75, 311, 20, 20), "")){
  63.             //GUI.skin = myGUISkin; ----------------------------22222222222222222222222222222222222
  64.             GUI.Label(new Rect(), "datosPersonaje.subirNum--");
  65.             datosPersonaje.indiceDemarcacion--;
  66.  
  67.             if (datosPersonaje.indiceDemarcacion <= 0){
  68.                 datosPersonaje.indiceDemarcacion = 8;
  69.             }
  70.         }
  71.  
  72.         GUI.skin = flechaDer;
  73.         if (GUI.Button(new Rect(Screen.width / 2 + 135, 311, 20, 20), "")){
  74.  
  75.             GUI.Label(new Rect(), "datosPersonaje.subirNum++");
  76.             datosPersonaje.indiceDemarcacion ++;
  77.  
  78.             if (datosPersonaje.indiceDemarcacion == 9){
  79.                 datosPersonaje.indiceDemarcacion = 0;
  80.             }
  81.         }
  82.  
  83.         GUI.skin = textoPorDefecto;
  84.  
  85.         GUI.Label(new Rect(Screen.width / 2 - 150, 340, 100, 25), "Numero Jug.");
  86.         //datosPersonaje.numeroJugador = GUI.TextField(new Rect(Screen.width / 2 - 50, 338, 50, 25), datosPersonaje.numeroJugador, 2);
  87.  
  88.         //Boton por si se quiere volver a la escena anterior "Informacion Usuario"
  89.         GUI.skin = myGUISkin;
  90.         if (GUI.Button(new Rect(Screen.width / 2 - 150, 380, 100, 25), "Volver")){
  91.  
  92.             Application.LoadLevel("2. Informacion Usuario");
  93.         }
  94.  
  95.         //Tras completar los datos del personaje, aceptar y avanzar a la proxima escena "Menu"
  96.         GUI.skin = myGUISkin;
  97.         if (GUI.Button(new Rect(Screen.width / 2 + 50, 380, 100, 25), "Aceptar")){
  98.  
  99.             Application.LoadLevel("Caracteristicas");
  100.         }
  101.  
  102.         GUILayout.EndArea();
  103.     }//Fin OnGUI
  104.  
  105. }//Fin clase DatosPersonaje

Por cierto, en este script la variable "datosPersonaje.indiceDemarcacion" que la utilizo para recorrer el array datosPersonaje.demarcacion[datosPersonaje.indiceDemarcacion], se que cuando termina el codigo mantiene el valor numerico de la posicion del array que el jugador haya escogido, (el numero 8 por ejemplo si se escoge la opcion "Delantero"), pero cuando en otro script quiero usar esa variable siempre se inicia a 0 (el uso en el otro script que digo es para pasar ese valor a otra variable que usare en un switch), ¿alquien ve en el script el por que?. La variable la declaro en la clase "Datos" y esta inicializada a 0 para que asi comience el array, pero si no le doy valor tambien sigue pasandome dicha variable a 0.

SCRIPT

Código C#:
Ver original
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Caracteristicas : MonoBehaviour {
  5.  
  6.     //ESCENA CARACTERISTICAS
  7.     public DatosPersonaje caractDatosPer;
  8.     public InformacionJugador caractInformacionJug;
  9.  
  10.     //Variables
  11.     public int posicionSwitch;
  12.     public int i; //Para recorrer los for
  13.  
  14.     void Start() {
  15.         caractDatosPer = new DatosPersonaje();
  16.         caractInformacionJug = new InformacionJugador();
  17.  
  18.         posicionSwitch = this.caractDatosPer.datosPersonaje.indiceDemarcacion;//Recoge el num del array "demarcacion" para usar en el swicht el case correspondiente
  19.     }
  20.  
  21.     void Update() {
  22.  
  23.     }
  24.  
  25.     void OnGUI () {
  26.  
  27.         GUI.Label(new Rect(Screen.width / 2 , 200, 760, 250), ""+posicionSwitch);
  28.  
  29.         //HABILIDADES
  30.         switch (posicionSwitch){
  31.  
  32.             case 0: //Portero
  33.  
  34.                 //GUILayout.BeginArea(new Rect (posGUI, GUILayout.Width(Screen.width * 0.2f), GUILayout.Height(Screen.height * 1f)));
  35.                 GUI.Label(new Rect(Screen.width / 2 - 370, 90, 120, 25), "Atrapar el balon");
  36.  
  37.                 for (i = 0; i < 10; i++){
  38.  
  39.                     int x = 0;
  40.                     x = Screen.width / 2 - 360;
  41.  
  42.                     GUI.DrawTexture(new Rect(x + (72 * i), - 195, 15, 25), ultimaHabilidad);
  43.                 }
  44.  
  45.                 GUI.Label(new Rect(Screen.width / 2 - 370, 120, 120, 25), "Reflejos");
  46.                 GUI.Label(new Rect(Screen.width / 2 - 370, 150, 120, 25), "Salto");
  47.                 GUI.Label(new Rect(Screen.width / 2 - 370, 180, 120, 25), "Agilidad");
  48.                 GUI.Label(new Rect(Screen.width / 2 - 370, 210, 120, 25), "Valentia");
  49.  
  50.                 break;
  51.  
  52.             case 1: //Defensa Central
  53.  
  54.                 break;
  55.  
  56.             ...............//El cogido esta acortado para que sea mas legible a los que veais este tema, dejo solo lo importante relaccionado con mi duda
  57.         }//Fin del switch
  58.  
  59.     }//Fin OnGUI
  60.  
  61.    
  62. }

Perdon que no hable con mucha propiedad, aun me queda un largo camino para hablar como un programador.

Última edición por Dempo87; 13/12/2014 a las 05:06