Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

picking random number from list

Discussion in 'Scripting' started by not_DiSE, Feb 22, 2019.

  1. not_DiSE

    not_DiSE

    Joined:
    Feb 18, 2019
    Posts:
    17
    hi , I was trying to make a code , that picks one of these numbers (11,12,13,14) ,but it says errors like :

    Assets\Sripts\RandomPick.cs(7,23): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

    and

    Assets\Sripts\RandomPick.cs(7,23): error CS0029: Cannot implicitly convert type 'float[*,*,*,*]' to 'float[]'

    what is bad ? code :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomPick : MonoBehaviour
    6. {
    7.     float[] numbers = new float[11f,12f,13f,14f];
    8.     int randomIndex = Random.Range(0, 3);
    9.         // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         float randomFLoatFromNumberst = numbers[randomIndex];
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.     }
    20. }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    You're defining array incorrectly;
    The arguments inside brackets are actually it's length.
    float[4] is an array of 4 floats, where as float[11f, 12f, 13f, 14f] would be 4-dimensional array with float amount of elements, which is not allowed.

    If you want to define an array, simply do it like so:
    Code (CSharp):
    1. float[] array = {1f, 2f, 3f};
     
  3. not_DiSE

    not_DiSE

    Joined:
    Feb 18, 2019
    Posts:
    17
    thx , it kinda works . It says
    UnityException: RandomRangeInt is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'RandomPick'.
    See "Script Serialization" page in the Unity Manual for further details.
    UnityEngine.Random.Range (System.Int32 min, System.Int32 max) (at C:/buildslave/unity/build/Runtime/Export/Random.bindings.cs:48)
    RandomPick..ctor () (at Assets/Sripts/RandomPick.cs:8)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomPick : MonoBehaviour
    6. {
    7.     float[] numbers = { 11f, 12f, 13f, 14f };
    8.     int randomIndex = Random.Range(1, 3);
    9.     public float picked;
    10.         // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         float randomFLoatFromNumbers = numbers[randomIndex];
    14.         picked = numbers[randomIndex];
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23. }
     
  4. not_DiSE

    not_DiSE

    Joined:
    Feb 18, 2019
    Posts:
    17
    OH and it picks only 11
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    Move Random.Range to the Start. Also, you can do Random.Range(0, numbers.Length) to pick any index in the array.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please don't mulitpost! 5 minutes I would like back :)