- Home /
Player not spawning in right place.
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);
}
}
Answer by LesPaul · Apr 22, 2013 at 06:01 PM
You need to save the PlayerPrefs each update in order to access them. Put this on the last line of your update function:
PlayerPrefs.Save();
It still does not work. No matter what it spawns in the same place all the time and its not mainspawn so i dont know i update the script.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Instantiate bullet towards Player position 1 Answer
swap between 3 gameobjects mid game 0 Answers
Stabilising users view in Unity VR (Fove HMD) 0 Answers
Saving System? 1 Answer