Select a prefab from a list variable using an index by an integer variable?
I'm using a button in a VR game to change the hand model of the player (a skins system)
and I created a public list of GameObjects that I've dragged the prefabs I need to choose from into. I've then created a public integer variable, labelled "Skin" that I can access from other gameobjects,
My issue is I need to use that integer "Skin" to select a prefab from that position in the list of GameObject and apply it to the model slot in another script. I figured out how to set the variable in another script, I just don't know how to pull that prefab out from the list of prefabs once it's put in.
I figured I could use a seperate variable, a private GameObject variable, and set that private variable to that index'd prefab from the list to then call later, I just don't know how. Any help would be a lifesaver!
Answer by firestorm185 · Jun 20, 2020 at 02:55 AM
figured it out! it was waaaaaay simpler than I thought. For anyone who finds this in the future, this is how you grab an item with an integer variable from a public list -
public list<"variable type here"> List1;
public "Variable type here" Object1;
public integer Skin = 0;
public void SkinChange()
{
Object1 = List1[Skin];
// This is the important part. You take the integer "Skin" in the list "List1" as the spot in the list you want
//to take the item from, and set it as equal to Object1, your premade variable. That way, once you place
//all your items in the list from Unity's GUI, you can access that object later and do whatever you want
//with it. Cheers!
}
Well I mean, it's "my answer" but ok Unity, go ahead, say it's the best. I won't judge. xD
Answer by N-8-D-e-v · Jun 20, 2020 at 02:39 AM
This is actually pretty simple, first create an integer variable, this will select the gameobject from the list
List<GameObject> yourList = New List<GameObject>();
int finder;
Then set that variable to a range between 0 and the length of your list (the first item in every list and array has an index of 0)
void Start()
{
finder = Random.Range(0, yourList.Length); //random number between first index and last index
}
NOTE random.range must be used in a method. After you've done that, just set that private gameobject variable you were talking about to the finder variable index in your list, like so
[Your Private GameObject Variable] = yourList[finder]; //this will get the item in the list with the index of whatever the random number was
And there you go! Let me know if you need anything else, cheers.
Thanks Curtz! I actually found an even simpler method to calling a specific item from a list. I just treat it like an Array and ins$$anonymous$$d of asking for a numbered index of the array, I put the integer variable I created in ins$$anonymous$$d! Like -
public list<Transform> List1;
public Transform Object1;
public integer Int1;
void Update()
{
Object1 = List1[Int1];
}
$$anonymous$$y issue was (and I've read a lot of different answers around that answered it similarly to your answer) was that my index position was chosen by a public variable that's set by an interaction with an XR component, not a random number, so I needed to know how to call for a specific variable at a specific number ins$$anonymous$$d of a random, which is all the answers I had been seeing. XD
Thanks again for the help though! I appreciate it!
I see, I haven't done anything with vr yet, but I'm happy to help!
Actually, while I'm here, could you point me towards any resources for game dev with vr?
Your answer
Follow this Question
Related Questions
Updating and redrawing list objects - How? 0 Answers
Only spawning power ups that the player wants in that game 1 Answer
How to add prefab children to a list in unity 0 Answers
Adding prefab objects to a list on another script and object? 2 Answers
For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers