- Home /
how can I tag a gameobject as Player at the start of a scene loading from the last?
I need to tag a gameobject the player selects from a character selection screen from the last scene in the next scene.
Can I do something like
Start Playerpref.SetTag= "Player"
From my knowledge, something like that is not possible.
Answer by David_Rios · Jan 02, 2018 at 03:51 AM
With assumption that you're using default Unity UI buttons, you can use the OnClick function with a referenced script to change the player's tag. The code would go like this; This includes referencing the player. I will show two separate functions giving the player different tags based on if they were selected or not.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TagScript : MonoBehaviour {
//GameObject you want as player
public GameObject Player;
public void WarriorTag()
{
Player.tag = "Warrior";
}
public void MageTag()
{
Player.tag = "Mage";
}
}
If you reference this script through the OnClick function and select the function representing the button, this should work fine. If you don't know how to do this, comment below. This will tag the GameObject the player selects if there are UI buttons on the selection screen. If they are objects that are to be chosen, then different code will be needed.
Will this require the player to press a button to tag? I already programmed the character selection. The player selects a character then it loads the scene and saves the selection in the Playerpref. Could I add a line of code in this script that says to "tag the player's selection " so the next scene makes that object the Player and all my scripts work fine because they need to find the player? All my scripts work fine when I manually toggle the player tag on the object I just need it to happen when the scene loads.
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");
}
}
Or maybe I could say that the objects in my array are "player" something like Player.tag= "CharacterList" in the script?
What gameobject's tag are you trying to change? What is the gameObject's name in the script?