vendredi 19 février 2021

How can I take a double array and randomize it? [closed]

I have an array which is a double type. Using that array I want to take random numbers from array and use them infinitely over and over.Unfortunately when I try to get these double values and use them I stuck. I don't get errors but I can't work it either. It supposed to give me double value numbers from array and implement it to spawning object's vectorZ.

to randomize I used this line:

randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];

I hope I give enough information :D thanks for reading.

HERE İS MY CODE:

public class instantiate : MonoBehaviour
{

public Transform barrierleft;  // copied objects position
private Vector3 nextbarrierleftSpawn; // spawning object
private double randomZ;               //  random value
double[] barrierleftList = {1.23, 4.56, 7.89}; // values

void Start()
{
    
 //implementig constant numbers these  values doesn't need randomising        
    nextbarrierleftSpawn.x=18;
    nextbarrierleftSpawn.y=4;
    StartCoroutine(spawnbarrierleft());  // startring coroutine to make it infinitely running
}

 IEnumerator spawnbarrierleft() // spawning a barrier infinitely.
{
    yield return new WaitForSeconds(1);
    
   //taking array and randomizing it.   PROBLEM!   is here I can't get this thing run! :(
    randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];

   //then taking randomized number to spawning object.
    nextbarrierleftSpawn.z = (float)randomZ;

   //lastly spawning object by given things.
    Instantiate(barrierleft, nextbarrierleftSpawn, barrierleft.rotation);

    nextbarrierleftSpawn.x += 8;
    StartCoroutine(spawnbarrierleft());
}

}




Aucun commentaire:

Enregistrer un commentaire