• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HuskyPanda213 · Apr 22, 2013 at 05:50 PM · positionplayerspawn

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HuskyPanda213 · Apr 22, 2013 at 08:59 PM 0
Share

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.

avatar image HuskyPanda213 · Apr 22, 2013 at 09:00 PM 0
Share

The updated script still does not work?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Character Born Position 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges