Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Foxi · Feb 09 at 10:58 AM · characterplayerprefssave datacharacter customization

PlayerPrefs. Load Selected Character from previous scene

Hi, so I've encountered a problem with my character selection.

To explain what I want to add: In main menu you can choose the character you want to play with. You click select. Then you can start the game with the character you selected in main menu, in previous scene.

What is happening: In main menu, you choose and click select the character, then you press play, another scene loads, but you play with a character you didn't select. The default character loads. Simply the PlayerPrefs isn't working.

I have my 8 characters stored in a parent GameObject. They are activating and deactivating as you choose them in main menu. The Same Script is also in another scene in the parent GameObject. So, how should I load the index character from one scene to another? I'm starting to get desperate and out of ideas. I have the same spcript on the same prefab "player" as you can see on the picture. But the index isn't choosen from the previous scene and "char1" is only active.

The Script for Character Selection:

[code=CSharp]

 public GameObject[] characterList;
 public int index;
 public SmoothCamera2D myCam;

 public void Start()
 {
     index = PlayerPrefs.GetInt("CharacterSelected");

     characterList = new GameObject[transform.childCount];
     for (int i = 0; i < transform.childCount; i++)
         characterList[i] = transform.GetChild(i).gameObject;

     foreach (GameObject go in characterList)
         go.SetActive(false);

     if (characterList[index])
         characterList[index].SetActive(true);
 }

 public void Left()
 {
     characterList[index].SetActive(false);

     index--;
     if (index < 0)
         index = characterList.Length - 1;
     characterList[index].SetActive(true);
     myCam.target = characterList[index].transform;
 }

 public void Right()
 {
     characterList[index].SetActive(false);

     index++;
     if (index == characterList.Length)
         index = 0;
     characterList[index].SetActive(true);
     myCam.target = characterList[index].transform;
 }

 public void SelectCharacter()
 {
     PlayerPrefs.SetInt("CharacterSelected", index);
      myCam.target = characterList[index].transform;
 }

[/code]

[1]: /storage/temp/192399-bez-nazvu.png

bez-nazvu.png (4.2 kB)
Comment
Add comment · Show 3
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 Cuttlas-U · Feb 09 at 12:29 PM 0
Share

please share both your menu and game functions witch are responsible for this and also use code tag to send cause right now I cant read it

avatar image Foxi Cuttlas-U · Feb 10 at 12:48 AM 0
Share

Edited the script so it's easier to read it. My bad, i'm sorry. The Last void SelectCharacter() is what i have for selecting the player in UI button. This script is on prefab "player" which I have the same as in menu so in the game. I thought the void Start() will do the thing in the game to get the index of the character and set it active.

avatar image Cuttlas-U · Feb 10 at 06:01 PM 0
Share

There is nothing wrong in this code , u did every thing on point .. when these kinds of problems happens your best shot is to print every thing and see witch data is not right ... like print the index value before saving it and print the value when loading in start function etc ...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Feb 11 at 11:20 AM

this line makes no sense:

  if (characterList[index])
      characterList[index].SetActive(true);

what are you trying to do? simply remove the if?

Comment
Add comment · 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

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

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

Related Questions

How do I save a previously bought characters in my Game? 0 Answers

Are playerprefs a safe way of saving data or not?? 2 Answers

How do I apply and save my game settings? 0 Answers

A Little Help With Player Prefs 0 Answers

Saving Players' Progress By Using PlayerPrefs 0 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