- Home /
Character selection
Hello, does anyone know how to make a character selection like the one in the Ballz mobile game? I have watched a YouTube tutorial on how to do it and came up with this script :
private List<GameObject> balls;
private int selectionIndex = 0;
private void Start()
{
balls = new List<GameObject>();
foreach(Transform t in transform)
{
balls.Add(t.gameObject);
t.gameObject.SetActive(false);
}
balls[selectionIndex].SetActive(true);
}
private void Update()
{
}
public void Select(int index)
{
if (index == selectionIndex)
return;
if (index < 0 || index >= balls.Count)
return;
balls[selectionIndex].SetActive(false);
selectionIndex = index;
balls[selectionIndex].SetActive(true);
}
Now, instead of the gameobjects to get inactive, what I want instead is to change the outline color of the gameobjects and change a text inside the gameobject. How would I do that? I'm still new to coding and have no idea.
Answer by Elisvaldo · Dec 28, 2017 at 12:30 PM
Most likely you'd like to add a component to the balls, something like BallSelectionBehaviour.
In that script you could have a public method called OnBallSelected()
, where you'd do everything that you want, including the SetActive()
.
And finally, in your script, you would call balls[selectionIndex].GetComponent<BallSelectionBehaviour>.OnBallSelected()
instead of the set active.
Note that GetComponent<>
can get a bit heavy. It might be a good idea to store the BallSelectionBehaviour
in a list, just like you did with the GameObject
, and populate the list only once on Start. Another option would be a Dictionary<GameObject, BallSelectionBehaviour>
.
From what I see, I'd also create a OnBallDeselected()
and add there the SetActive(false)
. It is a good idea to have your things separated. So, next time you want to add some particles, e.g., when a ball is selected, you know exactly where to change the code.
Hello, thank you for your reply. When I change the balls[selectionIndex].SetActive(true); to balls[selectionIndex].GetComponent.OnBallSelected(); it gives me an error - Expression denotes a method group', where a
variable', value' or
type' was expected.
You need to specify which component you want to get. In the case of my example, it would be balls[selectionIndex].GetComponent<BallSelectionBehaviour>().OnBallSelected()
. Ins$$anonymous$$d of BallSelectionBehaviour
just put the name of the $$anonymous$$onoBehaviour you have attached to the ball with the public method OnBallSelected().
Oh gosh, I'm stupid! Yeah, I'll try, thanks.
Your answer
Follow this Question
Related Questions
How to connect multiple stats scripts for the actual character stats? 1 Answer
How to Get Next One On The Que? 0 Answers
Unity networking create & join with custom character?, 0 Answers
How do I make Character Model to change across all scenes if changed in Main Menu? 1 Answer
Does anyone know how to delete selections in Unity 5? 2 Answers