Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - carlafelipe7

#1
General (Antiguo) / SceneManager
Mayo 23, 2018, 07:47:34 PM
Hola buenas, tengo problemas al pasar los puntos obtenidos en la escena 1 , a las siguientes escenas, he visto tutoriales, pero es algo antiguo ya que usa Application.loadedLevel == 2, 

Este codigo lo tengo en un script de C#, y en unity dentro de un Canvas, donde hay más datos, como un texto de puntuacion, un menu ...

Entonces, lo que intento hacer es que al recoger las monedas y me sume puntes en la pantalla1, al pasar a la 2, tenga esa misma puntuacion más la que recoga en la 2


Espero que se me haya ententido :S jaja,
 
Dejo por aqui los script y alguna imagen gracias
 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Puntuacion : MonoBehaviour {
    public int puntuacion;
    public Text puntosTexto;
// Use this for initialization
void Start () {
        if(PlayerPrefs.HasKey("Points")) {
          //Esto es lo que intento hacer, pero no me deja, ya que esa funcion ya no se usa, lo he intentado con SceneManager, pero no se como realizarlo.
            if(Application.loadedLevel == 2) {
                PlayerPrefs.DeleteKey("Points");
                puntuacion = 0;                        
            }
            else {
                puntuacion = PlayerPrefs.GetInt("Points");
            }
        }  
}
// Update is called once per frame
void Update () {
        puntosTexto.text = ("Points: " + puntuacion);
}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class PlayerControll : MonoBehaviour {
    public float maxSpeed = 100f;
public float speed = 300f;
public bool grounded;
public float jumpPlayer = 11f;
private Rigidbody2D rb2d;
private Animator anim;
private bool jump;
    private bool doubleJump;
    private bool movement = true;
    private SpriteRenderer spr;
    private SpriteRenderer color;  
    public int vidas;
    public int maxVidas=5;
    private Puntuacion pantallaPuntos;  
// Use this for initialization
void Start () {            
   rb2d = GetComponent<Rigidbody2D>();
   anim = GetComponent<Animator>();
        spr = GetComponent<SpriteRenderer>();
        vidas = maxVidas;
        pantallaPuntos = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<Puntuacion>();  
       
}
    // Update is called once per frame
    void Update() {
        //no tenga en cuenta que es pos o neg con Mathf.Abs
        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
        anim.SetBool("Grounded", grounded);
        //salto precaucion para saltar hacia atras o 2 veces
        if (grounded) {
            doubleJump = true;
        }
        if (Input.GetKeyDown(KeyCode.UpArrow)) {
            if (grounded) {
            jump = true;
            doubleJump = true;
            }
            else if (doubleJump){
                jump = true;
                doubleJump = false;
            }
        }
}
void FixedUpdate(){
   Vector3 fixedVelocity = rb2d.velocity;
   fixedVelocity.x *= 0.75f;
   if(grounded) {
      rb2d.velocity = fixedVelocity;
   }
   
   float h = Input.GetAxis("Horizontal");
        if (!movement) {
            h = 0;
        }
   rb2d.AddForce(Vector2.right * speed * h);
   
   //el mathf-Clamp para sinplificar las cosas
   float limitSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
   rb2d.velocity = new Vector2(-limitSpeed, rb2d.velocity.y);
   
   if(h > 0.1f) {
      transform.localScale = new Vector3(1f, 1f, 1f);
   }
   if(h < -0.1f) { //para que mire a la izq
      transform.localScale = new Vector3(-1f, 1f, 1f);
   }
   
   if(jump) {
      rb2d.velocity = new Vector2(rb2d.velocity.x, 0); //cancela la velc vertical
      rb2d.AddForce(Vector2.up * jumpPlayer, ForceMode2D.Impulse);
            jump = false;          
   }  
       
        if(vidas > maxVidas) {
            vidas = maxVidas;
        }
        if(vidas <= 0) {
            Die();
        }
   //debug en consola
   //Debug.Log(rb2d.velocity.x);
}
    void Die() {
        //emepezar de nuevo
        SceneManager.LoadScene("Principal");      
    }
    void OnBecameInvisible() {//cuando pierda o se caiga vuelve a esa posicion
        transform.position = new Vector3(-4, 0, 0);
    }
    public void EnemyJump() {
        jump = true;
    }
    //retroceso al satar
    public void EnemyKnockBack(object enemyPosX) {
        float posicion = (float)enemyPosX;
        jump = true;
        float side = Mathf.Sign(posicion - transform.position.x);
        rb2d.AddForce(Vector2.left * side * jumpPlayer, ForceMode2D.Impulse);
        movement = false;
        Invoke("EnableMovement", 0.9f);
        Color color = new Color(255/255f, 106/255f, 0/255f);
        spr.color = color;
    }
    void EnableMovement() {
        movement = true;
        spr.color = Color.white;
    }
    public void Damage(int dano) {
        maxVidas -= dano;
    }
    public void OnTriggerEnter2D(Collider2D col) {
        if(col.CompareTag("Coin")) {
            Destroy(col.gameObject);
            pantallaPuntos.puntuacion += 5;
        }
    }
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class changeNivel : MonoBehaviour {
    public int level;                      
    private Puntuacion pantallaPuntos;
    private void Start() {
        pantallaPuntos = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<Puntuacion>();
    }
    void OnTriggerEnter2D(Collider2D other) {
        if (other.CompareTag("Player")) {
            GuardarPuntuacion();
            SceneManager.LoadScene(level);
        }
    }
    void GuardarPuntuacion() {
        PlayerPrefs.SetInt("Points", pantallaPuntos.puntuacion);
    }
}

 
 
 
 
 
Adjunto alguna imagen para que se pueda ver...

Gracias