Using FindObjectsOfType to create a randomise all features button on a character creator?,
I am a beginner at this, and am creating this project purely out of a desire to learn, if I've missed something that is commonly obvious and/or phrased this in the least efficient way, I apologise, please assume I know nothing.
I'm trying to create a character creation menu in unity. Things are going well so far, but I've decided I would like to add a button that randomises all the features of the character. I have managed to get one item to change randomly when the button is pressed, but not multiple. All items have the same script attached to them ("ChangingFeatures" script) and I am using this to tell the menu what to do when the button is pressed (pick something from a random range and change the sprite). To achieve this one item changing, I used FindObjectOfType(); which is great, and works fine, yay - one object achieved! Unfortunately this doesn't work to change all objects at once. I was told FindObjectsOfType would work but I don't understand how? My question is, is there a way to use FindObjectsOfType to change all of them at once, without having to go through and assign a different script to each item of clothing? This only returns an array and I'm not sure how that helps in changing the arrays in the ChangingFeatures script that store what item the character is wearing.
What I'm using to reference the feature
private ChangingFeature changeFeature;
private void Start()
{
changeFeature = FindObjectsOfType<ChangingFeature>();
}
The start of the ChangingFeatures script, where the arrays are stored, I want to be able to change the feature, featureText, options, index, optionsText and textIndex all to the same thing, at the same time, across the board from every manager that holds the ChangingFeatures script when the Randomize button is pressed.
public class ChangingFeature : MonoBehaviour
{
public SpriteRenderer feature;
public Text featureText;
public Sprite[] options;
public int index;
public string[] optionsText;
public int textIndex;
}
,
Answer by Pocknell-r251 · Jul 01, 2020 at 03:08 PM
I was doing this completely wrong. For anyone who might've stumbled across this who followed similar wrongness, check out this tutorial, the answer is to not use FindObjectOfType at all, but rather to use a list and to assign all your characters parts to that list. link text