So i need help with randomly picking string,int,float,gameobject and transform from array… so now i need to pick random fish module from the array, and the same for the other variables. Can anyone help me? i tried so many tutorials but they dont seem to work with me…
A typical way to choose a random thing out of an array is
int randomIndex = UnityEngine.Random.Range(0, myArray.Length);
var randomItem = myArray[randomIndex];
but it doesnt work with gameobjects
It works. You just need array of GameObjects.
It works exactly the same no matter what type of things are in your array. You have probably misunderstood the source of some error you are getting.
Why don’t you try posting some code that you’ve tried but doesn’t work, and we can advise you on what’s wrong with it?
1 Like
Error CS0029 Cannot implicitly convert type ‘int’ to ‘UnityEngine.GameObject’
Code :
public GameObject[] fishModels;
void Start(){
GameObject randomIndex = UnityEngine.Random.Range(0, fishModel.Length);
}
You missed one step.
Index is an int.
You pass index int to get element of an array.
1 Like
int randomIndex = UnityEngine.Random.Range(0, fishModels.Length);
GameObject randomObject = fishModels[randomIndex];
1 Like