Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

checkPoint & playerpref

Discussion in 'Editor & General Support' started by kenanimohamed, Jan 29, 2021.

  1. kenanimohamed

    kenanimohamed

    Joined:
    Jan 19, 2021
    Posts:
    7
    hello, can someone help me with a problem I've been blocking for a few days and found nothing on the net.

    So there you go, I'm training to design a Unity 2D game, Mario type. and I want to make a checkpoint system. Thus, the player could revive at the last chechpoint after seeing an advertisement (Rewarded AD). So the pub part, it works, after viewing, there is "reloading the part" and replacing the character at the first checkpoint.

    the problem is: it always comes back to the first checkpoint, it does not save the coordinates of the next checkpoints, I have tried everything: I put the checkpoints in prefabs, I took them out, I tried to do them separately, I tried a courotine, to erase the data and reload them after a new collision and nothing helps, it always comes back to the first checkpoint. this is my, my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CheckPoint : MonoBehaviour
    6. {
    7.    
    8.      private void OnTriggerEnter2D(Collider2D collision){
    9.          if(collision.CompareTag("Player")){
    10.             PlayerPrefs.SetFloat("x",gameObject.transform.position.x);
    11.             PlayerPrefs.SetFloat("y",gameObject.transform.position.y);
    12.             PlayerPrefs.SetFloat("z",gameObject.transform.position.z);
    13.          }
    14.      }
    15.    
    16.      public void Update(){
    17.  
    18.        if(PlayerPrefs.GetInt("isRewarded")== 1  ){
    19.  
    20.             float x = PlayerPrefs.GetFloat("x");
    21.             float y = PlayerPrefs.GetFloat("y");
    22.             float z = PlayerPrefs.GetFloat("z");    
    23.             GameObject.FindGameObjectWithTag("Player").transform.position = gameObject.transform.position;
    24.             GameOverManager.instance.isRewarded = 0;
    25.             PlayerPrefs.SetInt("isRewarded", 0);
    26.         }        
    27.     }
    28.  
    29. }
    30.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,760
    Lines 20 to 22 above... you get x,y,z to local variables, then do nothing with those values.
     
  3. kenanimohamed

    kenanimohamed

    Joined:
    Jan 19, 2021
    Posts:
    7
    hey, yes somoene else told me the same thing, so i ve found a way and its work now, i ll put here :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class takeCheckpoint : MonoBehaviour
    6. {
    7.    
    8.      public Transform checkpoint;
    9.    
    10.      public float posX;
    11.     public float posY;
    12.      public float posZ;
    13.  
    14.      public void Start(){
    15.  
    16.              posX = checkpoint.position.x;    
    17.              posY = checkpoint.position.y;
    18.              posZ = checkpoint.position.z;
    19.  
    20.      }
    21.      public void OnTriggerEnter2D(Collider2D collision){
    22.          if(collision.CompareTag("Player")){
    23.             PlayerPrefs.SetFloat("x",posX);
    24.             PlayerPrefs.SetFloat("y",posY);
    25.             PlayerPrefs.SetFloat("z",posZ);
    26.             }
    27.           }
    28.  
    29. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CheckPoint : MonoBehaviour
    6. {
    7.     public takeCheckpoint positionCheck;
    8.    
    9.     public void Start(){
    10.  
    11.  
    12.  
    13.      }
    14.    
    15.      public void Update(){
    16.  
    17.        if(PlayerPrefs.GetInt("isRewarded")== 1  ){
    18.          
    19.             Debug.Log(positionCheck.posX);
    20.  
    21.             positionCheck.posX = PlayerPrefs.GetFloat("x");
    22.             positionCheck.posY = PlayerPrefs.GetFloat("y");
    23.             positionCheck.posZ = PlayerPrefs.GetFloat("z");    
    24.             GameObject.FindGameObjectWithTag("Player").transform.position = new Vector3(positionCheck.posX,positionCheck.posY,positionCheck.posZ);
    25.             GameOverManager.instance.isRewarded = 0;
    26.             PlayerPrefs.SetInt("isRewarded", 0);
    27.         }        
    28.     }
    29.    
    30. }
    thank you anyway ;)
     
    Kurt-Dekker likes this.