- Home /
Why is my character selection menu not working.
So basically i'm trying to create a character selection menu and this is my code so far(and by my code i mean the code i stole from a random youtuber) 1st script using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class Character
{
public Sprite characterSprite;
}
2nd script using System.Collections; using System.Collections.Generic; using UnityEngine;
[CreateAssetMenu]
public class CharacterDataBase : ScriptableObject
{
public Character[] character;
public int characterCount
{
get
{
return character.Length;
}
}
public Character GetCharacter(int index)
{
return character[index];
}
}
3rd script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CharacterManager : MonoBehaviour
{
public CharacterDataBase characterDb;
public SpriteRenderer artworkSprite;
private int selectedOption = 0;
// Start is called before the first frame update
void Start()
{
UpdateCharacter(selectedOption);
}
public void NextOption()
{
selectedOption ++;
if(selectedOption >= characterDb.characterCount)
{
selectedOption = 0;
}
UpdateCharacter(selectedOption);
Save();
}
public void BackOption()
{
selectedOption --;
if (selectedOption < 0)
{
selectedOption = characterDb.characterCount - 1;
}
UpdateCharacter(selectedOption);
Save();
}
private void UpdateCharacter(int selectedOption)
{
Character character = characterDb.GetCharacter(selectedOption);
artworkSprite.sprite = character.characterSprite;
}
private void Load()
{
selectedOption = PlayerPrefs.GetInt("selectedOption");
}
private void Save()
{
PlayerPrefs.SetInt("selectedOption", selectedOption);
}
public void ChangeScene(int sceneID)
{
SceneManager.LoadScene(sceneID);
}
}
4th script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public CharacterDataBase characterDb;
public SpriteRenderer artworkSprite;
private int selectedOption = 0;
// Start is called before the first frame update
void Start()
{
UpdateCharacter(selectedOption);
}
private void UpdateCharacter(int selectedOption)
{
Character character = characterDb.GetCharacter(selectedOption);
artworkSprite.sprite = character.characterSprite;
}
private void Load()
{
selectedOption = PlayerPrefs.GetInt("selectedOption");
}
}
Okay so the first 2 scripts are just for declaring variables and to create the data base in game, so they're not attachable so any game object.
The 3rd script is applied to the sprite in the "Menu" Scene, and it basically allows you to browse all the sprites available(the ones in the database created above)
Then finally the 4th script is attached to the sprite in the actual game scene.
Now the problem is whatever i choose in the selection menu, after pressing the play button it always shows the same character which is element 0 in the data base(so basically the first sprite) the scripts are working since the element 0 sprite is not the one i placed in the game scene so i'm guessing the problem is "selectedOption" is always set to 0 for some reason.
Answer by NeatFreak · May 19 at 01:58 PM
Because you're not loading the PlayerPrefs value.
Call Load() at the Start() for CharacterManager and Player scripts.
Thank youuuuuu! Kinda dumb question ig so sorry for that hahah.
I’ve done this. Call load() at the start for charmanager and player scripts but it wont load my second character. Help please :(
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Toggling a sprite renderer? 2 Answers
How can I change the Value of a Color through script? 1 Answer
Sprite Swapping help 1 Answer