- Home /
Can I set a player tag to this line of code?
Playerpref.SetInt(“CharacterSelected”,Index);
I want the line of code to make it so the character selected gets tag “Player”
Yes, it is a character selection I saved in the playerpref. it saves the players character selection and loads it to the next scene. I just need that character selected and stored in the playerpref to be tagged "player" at the start of the scene.
Answer by OneCept-Games · Jan 02, 2018 at 04:09 PM
PlayerPrefs is for storing data on persistant media (like a disk, or static memory). If you want to set the tag attribute of an object, you should use playerObject.tag = "Player" or similar.
Yes, it is a character selection I saved in the playerpref. it saves the players character selection and loads it to the next scene. I just need that character selected and stored in the playerpref to be tagged "player" at the start of the scene.
ok, then you could set the tag attribute on your character object, like: characterObject[Index].tag = PlayerPrefs.GetInt("CharacterSelected");
I am though not sure what the index is for here, so I assumed you had it as an index of an array. Just make sure you are having the same order in the array when you read back the value.
Here is the code I have for the character selection. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class CharacterSelection : $$anonymous$$onoBehaviour {
private GameObject[] characterList;
private int index; // keeping track of which model we are looking at
//Get reference to every single object/ character
private void Start ()
{
index = PlayerPrefs.GetInt ("CharacterSelected");
characterList = new GameObject[transform.childCount];
// Fill the array with our models
for (int i = 0; i < transform.childCount; i++)
characterList [i] = transform.GetChild (i).gameObject;
// We toggle off their renderer
foreach (GameObject go in characterList)
go.SetActive (false);
// we togggle on the selected Character
if (characterList [index])
characterList [index].SetActive (true);
}
public void ToggleLeft()
{
// Toggle off the current model
characterList[index].SetActive(false); // turning off index object
index --; // Or you could write index --; or index= index -1;
if (index < 0)
index = characterList.Length - 1; // security check to see if we are not out of array range. This brings you back to end of index
// toggle on the current model
characterList[index].SetActive(true);// turnning ON index object
}
public void ToggRight()
{
// Toggle off the current model
characterList[index].SetActive(false); // turning off index object
index ++; // Or you could write index --; or index= index -1;
if (index == characterList.Length)
index = 0; // security check to see if we are not out of array range. This brings you back to end of index
// toggle on the current model
characterList[index].SetActive(true);// turnning ON index object
}
public void ChangeSceneButton() // public functions because we call them from buttons
{
PlayerPrefs.SetInt ("CharacterSelected", index); // transfer charect date to next scene by storing character information in a prefab
Scene$$anonymous$$anager.LoadScene ("Gameplay lvl");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player will not pick up items 1 Answer
I Want My Player To Be Sent Flying In The Air When He Get's Hit By An Enemy 1 Answer
Boosting Character Controller Problem 0 Answers