Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error al Compilar

Discussion in 'Scripting' started by Nixel2013, May 17, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    I have this script with which I use it to make a "saved in the place where the character is", but the problem is that when I build the project does not fulfill the function that should, however in unity if it works. I would like it if they have any solution or because of the error that causes the script not to work

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GuardadoStatico : MonoBehaviour
    6. {
    7.     public Transform Player;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.         if(PlayerPrefs.GetFloat("X") != 0.0f)
    13.         {
    14.             Player.position = new Vector3(PlayerPrefs.GetFloat("X"), PlayerPrefs.GetFloat("Y"), PlayerPrefs.GetFloat("Z"));
    15.         }
    16.     }
    17.  
    18.     void Update()
    19.  
    20.     {
    21.         PlayerPrefs.SetFloat("X", Player.position.x);
    22.         PlayerPrefs.SetFloat("Y", Player.position.y);
    23.         PlayerPrefs.SetFloat("Z", Player.position.z);
    24.  
    25.  
    26.     }
    27. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Depending on what device you run this on, you may want to invoke PlayerPrefs.Save() after changing x, y, and z in Update().

    Also note that during start, you are doing something dangerous: you compare a float to not being zero, after calling GetFloat which you do not know if it exists.

    A better way of doing this is would be

    Code (CSharp):
    1. if (PlayerPrefs.HasKey("X") {
    2.    ... // read x, y, z
    3. }