I want an array of double numbers (1.64, etc.). I want to take them..

Feb 15 2021 10:53 PM
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 :
  1. var element = myArray[Random.Range(0, myArray.Length)];  
  2. // this sort of thing. As it seems that it doesn't get the double .  
so look (List<> ) but it also problematic whenever I try adding a double value it gives an absurd error so I decided to use :
  1. double[] barrierleftList = {1.23, 4.56, 7.89};  
  2. //14th line  
but I have some problem with that too
  1. randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];  
  2. //46th line  
// 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)
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class instantiate : MonoBehaviour  
  6. {  
  7. public Transform bars;  
  8. private Vector3 nextBarsSpawn;  
  9. public Transform barrierleft;  
  10. public Transform barrierright;  
  11. private Vector3 nextbarrierleftSpawn;  
  12. private Vector3 nextbarrierrightSpawn;  
  13. private int randomZ;  
  14. double[] barrierleftList = {1.23, 4.56, 7.89};  
  15. double[] barrierrightList = {1.23, 4.56, 7.89};  
  16. /* List<double> nextbarrierleftList = new List<double>(); 
  17. items.Add(1.23); 
  18. items.Add(4.56); 
  19. items.Add(7.89); 
  20. // This will give you a double[3] array with the items of the list. 
  21. double[] barrierleftList = barrierleftList.ToArray(); 
  22. */  
  23. void Start()  
  24. {  
  25. nextBarsSpawn.x=24;  
  26. nextBarsSpawn.y=4;  
  27. nextbarrierleftSpawn.x=18;  
  28. nextbarrierleftSpawn.y=4;  
  29. nextbarrierrightSpawn.x=18;  
  30. nextbarrierrightSpawn.y=4;  
  31. StartCoroutine(spawnBars());  
  32. StartCoroutine(spawnbarrierleft());  
  33. //StartCoroutine(spawnbarrier());  
  34. }  
  35.   
  36. // Update is called once per frame  
  37. void Update()  
  38. {  
  39. }  
  40. IEnumerator spawnbarrierleft()  
  41. {  
  42. // left barrier  
  43. yield return new WaitForSeconds(1);  
  44. //randomZ=Random.Range(-1,2);  
  45. randomZ = barrierleftList[Random.Range(0, barrierleftList.Length)];  
  46. nextbarrierleftSpawn.z=randomZ;  
  47. Instantiate(barrierleft, nextbarrierleftSpawn, barrierleft.rotation);  
  48. nextbarrierleftSpawn.x += 8;  
  49. StartCoroutine(spawnbarrierleft());  
  50. }  
  51. IEnumerator spawnBars()  
  52. {  
  53. yield return new WaitForSeconds(1);  
  54. Instantiate(bars, nextBarsSpawn, bars.rotation);  
  55. nextBarsSpawn.x += 8;  
  56. StartCoroutine(spawnBars());  
  57. }  
  58. }  

Answers (1)