Unity Spain
Post Antiguos => General (Antiguo) => Mensaje iniciado por: Argo214 en Enero 19, 2016, 12:19:45 PM
Buenas! Estoy intentando añadir un sistema de guardado y cargado de la posición y puntuación del player de mi juego, pero me salta este error. ¿Cómo puedo solucionarlo?"Static member `PuntosPistas.puntuacion' cannot be accessed with an instance reference, qualify it with a type name instead"Este es el código que tengo:using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GuardarCargar : MonoBehaviour {
GameObject Player;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
if (CargarPartida.Cargando)
{
Player.transform.position = new Vector3(PlayerPrefs.GetFloat("Player x"), 0, PlayerPrefs.GetFloat("Player z"));
Player.GetComponent<PuntosPistas>().puntuacion = PlayerPrefs.GetInt("puntuacion");
}
}
public void Guardar()
{
PlayerPrefs.SetFloat("Player x", Player.transform.position.x);
PlayerPrefs.SetFloat("Player z", Player.transform.position.z);
PlayerPrefs.SetInt("puntuacion", Player.GetComponent<PuntosPistas>().puntuacion);
PlayerPrefs.Save();
}
public void Borrar()
{
PlayerPrefs.DeleteKey("Player x");
PlayerPrefs.DeleteKey("Player z");
PlayerPrefs.SetInt ("puntuacion", Player.GetComponent<PuntosPistas>().puntuacion);
}
} Y la variable "puntuación" está en esta otra script:using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PuntosPistas : MonoBehaviour {
public static int puntuacion;
public Text TextoContador;
private int pista1;
private int pista2;
private int pista3;
private int pista4;
void Start ()
{
pista1 = PlayerPrefs.GetInt("pista1");
pista2 = PlayerPrefs.GetInt("pista2");
pista1 = PlayerPrefs.GetInt("pista3");
pista2 = PlayerPrefs.GetInt("pista4");
TextoContador.text = "" + puntuacion;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "pista1"){
other.gameObject.SetActive(false);
if (pista1 == 1)
return;
puntuacion = puntuacion + 5;
TextoContador.text = "" + puntuacion;
PlayerPrefs.SetInt("pista1", 1);
}.................
Es que puntuacion la has definido como static. Esto significa que no es un objeto instanciable, por tanto necesitas acceder a él con el qualifier, o sea el nombre de la clase: PuntosPistas.puntuacionEntiendo que sabes lo que haces, ya que la clase no es static y contiene otras variables no static.
Soy bastante novato en todo esto y estoy aprendiendo. La variable "puntuación" la tengo configurada como static porque se mantiene en todas las escenas del juego.
He sustituido puntuacion por PuntosPistas.puntuacion como dices, pero sigue dando errores. Supongo que no lo estaré haciendo bien. Te agradecería mucho que me ayudaras a encontrar una solución. Gracias!!using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GuardarCargar : MonoBehaviour {
GameObject Player;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
if (CargarPartida.Cargando)
{
Player.transform.position = new Vector3(PlayerPrefs.GetFloat("Player x"), 0, PlayerPrefs.GetFloat("Player z"));
Player.GetComponent<PuntosPistas>().PuntosPistas.puntuacion = PlayerPrefs.GetInt("puntuacion");
}
}
public void Guardar()
{
PlayerPrefs.SetFloat("Player x", Player.transform.position.x);
PlayerPrefs.SetFloat("Player z", Player.transform.position.z);
PlayerPrefs.SetInt("puntuacion", Player.GetComponent<PuntosPistas>().PuntosPistas.puntuacion);
PlayerPrefs.Save();
}
public void Borrar()
{
PlayerPrefs.DeleteKey("Player x");
PlayerPrefs.DeleteKey("Player z");
PlayerPrefs.SetInt ("puntuacion", Player.GetComponent<PuntosPistas>().PuntosPistas.puntuacion);
}
}