Hello Im trying to make my player spawn in the place that he left the game in but he is always spawning in mainspawn no matter what I don’t know whats wrong I checked all the tags and they are correct please Someone Help!!!
Heres my script:
using UnityEngine;
using System.Collections;
public class StatTracker : MonoBehaviour {
public float PosX;
public float PosY;
public float PosZ;
public GameObject SpawnMain;
public GameObject Player;
public GameObject PlayerPre;
public Vector3 PlayerPos;
void Start(){
SpawnMain = GameObject.FindGameObjectWithTag("MainSpawn");
PosX = PlayerPrefs.GetFloat("X");
PosY = PlayerPrefs.GetFloat("Y");
PosZ = PlayerPrefs.GetFloat("Z");
PlayerPos.x = PosX;
PlayerPos.y = PosY;
PlayerPos.z = PosZ;
if(PlayerPos.z != 0 && PlayerPos.x != 0){
Instantiate(PlayerPre,PlayerPos,Quaternion.identity);
}
else{
Instantiate(PlayerPre,SpawnMain.transform.position,Quaternion.identity);
}
}
void Update(){
Player = GameObject.FindGameObjectWithTag("Player");
PosX = Player.transform.position.x;
PosY = Player.transform.position.y;
PosZ = Player.transform.position.z;
PlayerPrefs.SetFloat("X",PosX);
PlayerPrefs.SetFloat("Y",PosY);
PlayerPrefs.SetFloat("Z",PosZ);
}
}
Updated Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class StatTracker : MonoBehaviour {
public float PosX;
public float PosY;
public float PosZ;
public Vector3 Spawn;
private bool isPaused = false;
private bool isPauseMenu = true;
public GameObject Prefab_Player;
public GameObject Spawn_1;
public GameObject Player;
void Start(){
PosX = PlayerPrefs.GetFloat("X");
PosY = PlayerPrefs.GetFloat("Y");
PosZ = PlayerPrefs.GetFloat("Z");
Spawn.x = PosX;
Spawn.y = PosY;
Spawn.z = PosZ;
if(Spawn != Vector3.zero){
Instantiate(Prefab_Player,Spawn,Quaternion.identity);
}
else{
Instantiate(Prefab_Player,Spawn_1.transform.localPosition,Quaternion.identity);
}
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update(){
Player = GameObject.FindGameObjectWithTag("Player");
PlayerPrefs.SetFloat("X",Player.transform.position.x);
PlayerPrefs.SetFloat("Y",Player.transform.position.y);
PlayerPrefs.SetFloat("Z",Player.transform.position.z);
}
}