Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 jmparavicini · Nov 08, 2014 at 08:57 PM · positionplayerprefs

PlayerPrefs Player And Enemy Spawn

Hello Everyone

I made some scripts:

Save Button:

 var Saver : Saver;
 
 function OnGUI()
 {
     if(GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 2 + 65, 100, 70), "Save"))
     {
                 Saver.Save();
     }
 }


The EnemyPositions:

 #pragma strict
 
 static var PlayerX : float;
 static var PlayerY : float;
 static var PlayerZ : float;
 
 static var EnemyX : float;
 static var EnemyY : float;
 static var EnemyZ : float;
 
 var enemeeeeyX : Vector3;
 
 var player2 : GameObject;
 var enemy2 : GameObject;
 
 static var player : GameObject;
 static var enemy : GameObject;
 
 var PlayerPosition : Transform;
 var EnemyPosition : Transform;
 
 function Update () 
 {
     enemeeeeyX = EnemyPosition.transform.position;
     player = player2;
     enemy = enemy2;
     
     EnemyX = EnemyPosition.transform.position.x;
     EnemyY = EnemyPosition.transform.position.y;
     EnemyZ = EnemyPosition.transform.position.z;
 
     PlayerX = PlayerPosition.transform.position.x;
     PlayerY = PlayerPosition.transform.position.y;
     PlayerZ = PlayerPosition.transform.position.z;
     
     savePosition();
 }


The Actual Saver Script:

 #pragma strict
 
 function OnTriggerEnter () 
 {
     Save();
 }
 
 function Save()
 {
     PlayerPrefs.SetFloat("Health", playerHealth.Health);
     PlayerPrefs.SetFloat("Energy", Flashlight.energy);
     
     PlayerPrefs.SetFloat("EnemyX", Loader.EnemyX);
     PlayerPrefs.SetFloat("EnemyY", Loader.EnemyY);
     PlayerPrefs.SetFloat("EnemyZ", Loader.EnemyZ);
     
     PlayerPrefs.SetFloat("PlayerX", Loader.PlayerX);
     PlayerPrefs.SetFloat("PlayerY", Loader.PlayerY);
     PlayerPrefs.SetFloat("PlayerZ", Loader.PlayerZ);
     
     PlayerPrefs.SetInt("Battery", Flashlight.Battery);
 }

And Then The Spawner:

 function OnGUI()
 {
     if(GUI.Button(Rect(Screen.width / 2 - 600, Screen.height / 2 + 100, 210, 70), "Load Game") && PlayerPrefs.HasKey("Spawn") && PlayerPrefs.GetString("Spawn") == "true")
     {
         level1load();
     }
     
     if(GUI.Button(Rect(Screen.width / 2 - 600, Screen.height / 2 + 100, 210, 70), "Load Game"))
     {
         level1new();
     }
 }
 
 function level1new()
 {
     Application.LoadLevel(3);
     playerHealth.Health = 100;
     Flashlight.Battery = 0;
     Flashlight.energy = 100;
     
     Loader.PlayerX = 41;
     Loader.PlayerY = 1.5;
     Loader.PlayerZ = 34;
     
     Loader.EnemyX = 26;
     Loader.EnemyY = 1.5;
     Loader.EnemyZ = 34;
     
     Flashlight.Battery = 0;
     
     PlayerPrefs.SetString("Spawn", "true");
 }
 
 function level1load()
 {
     Application.LoadLevel(3);
     playerHealth.Health = PlayerPrefs.GetFloat("Health");
     
     Loader.PlayerX = PlayerPrefs.GetFloat("PlayerX");
     Loader.PlayerY = PlayerPrefs.GetFloat("PlayerY");
     Loader.PlayerZ = PlayerPrefs.GetFloat("PlayerZ");
     
     Loader.EnemyX = PlayerPrefs.GetFloat("EnemyX");
     Loader.EnemyY = PlayerPrefs.GetFloat("EnemyY");
     Loader.EnemyZ = PlayerPrefs.GetFloat("EnemyZ");
     
     Flashlight.Battery = PlayerPrefs.GetInt("Battery");
     
     Flashlight.energy = PlayerPrefs.GetFloat("Energy");
 }




When I Play a new Game everything works fine but when i hit loadGame then everythings loads except the player and enemy they spawn at the newGamePosition and i dont know why. it should work i think

ps: All Buttons work. And the enemeeeeeeeeyX : Vector3 give me the coordinates 0,0,0 so something must be wrong in the EnemyPosition Script but i cant figure it out

Please Help me

thanks in advance skullbeats1

Comment
Add comment · Show 1
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 RemiR · Nov 08, 2014 at 11:05 PM 0
Share

You should use PlayerPrefs.Save() in your Saver script.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SquigglyFrog · Nov 08, 2014 at 10:19 PM

Mind you, I'm at work and just glancing at this, but when I look through that code, I see you loading your positions into variables like Loader.PlayerY ... but I never see you assigning those to the position.. it almost looks like your missing a statement to set the position after you load it.. Something like this, or whatever the javascript equivalent is.. same with your enemy.

player.transform.position = new vector3(Loader.PlayerX, Loader.PlayerY, Loader.PlayerZ);

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 jmparavicini · Nov 09, 2014 at 01:53 PM 0
Share

When i look at my game's plist then i see that the playerX etc. never change why is that so??

avatar image jmparavicini · Nov 09, 2014 at 02:29 PM 0
Share

i figured it out

thank you and you too RemiR

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.

Follow this Question

Answers Answers and Comments

28 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 avatar image avatar image avatar image avatar image 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

Reloading level or re-positioning player and objects? 2 Answers

PlayerPrefs Saving Player's Position Java Script 2 Answers

Camera changes position? 1 Answer

Unable to set Rotation 1 Answer

How to load a scene with playerprefs from a ui element 1 Answer


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