I have an array of double numbers (1.64, etc.). I want to take them randomly and use it to instantiate in that position(number). I looked up but what I have found :
- var element = myArray[Random.Range(0, myArray.Length)];
-
so look (List<> ) but it also problematic whenever I try adding a double value it gives an absurd error so I decided to use :
- double[] barrierleftList = {1.23, 4.56, 7.89};
-
but I have some problem with that too
- randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];
-
// when I run this it gives:
Assets\instantiate.cs(46,19): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
This is my problem.
btw Thanks even for reading :)
HERE IS MY FULL CODE! ( why here you might ask because it is easier to follow)
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class instantiate : MonoBehaviour
- {
- public Transform bars;
- private Vector3 nextBarsSpawn;
- public Transform barrierleft;
- public Transform barrierright;
- private Vector3 nextbarrierleftSpawn;
- private Vector3 nextbarrierrightSpawn;
- private int randomZ;
- double[] barrierleftList = {1.23, 4.56, 7.89};
- double[] barrierrightList = {1.23, 4.56, 7.89};
-
-
-
-
-
-
-
- void Start()
- {
- nextBarsSpawn.x=24;
- nextBarsSpawn.y=4;
- nextbarrierleftSpawn.x=18;
- nextbarrierleftSpawn.y=4;
- nextbarrierrightSpawn.x=18;
- nextbarrierrightSpawn.y=4;
- StartCoroutine(spawnBars());
- StartCoroutine(spawnbarrierleft());
-
- }
-
-
- void Update()
- {
- }
- IEnumerator spawnbarrierleft()
- {
-
- yield return new WaitForSeconds(1);
-
- randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];
- nextbarrierleftSpawn.z=randomZ;
- Instantiate(barrierleft, nextbarrierleftSpawn, barrierleft.rotation);
- nextbarrierleftSpawn.x += 8;
- StartCoroutine(spawnbarrierleft());
- }
- IEnumerator spawnBars()
- {
- yield return new WaitForSeconds(1);
- Instantiate(bars, nextBarsSpawn, bars.rotation);
- nextBarsSpawn.x += 8;
- StartCoroutine(spawnBars());
- }
- }