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

change this script so i can have an array of names instead of number

Discussion in 'Scripting' started by matstacy, Feb 7, 2020.

  1. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    i have run this code and it is working fine with numbers. is there an easy way i can add a big list of names to get randomly picked. thanks alot

    using UnityEngine;
    using System.Collection;
    using UnityEngine.UI;

    public class RandomGenerator: MonoBehaviour{

    public GameObject TextBox;
    public int TheNumber;

    public void RandomGenerate() {
    TheNumber= Random.Range (1, 10000);
    TextBox.GetComponent<text> ().text= TheNumber;

    }
    }
     
  2. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    You can create an array of strings
    public string[] names;


    Then fill the array in the inspector with all the names you want to choose from
    Then you can use this function to pick a random from the array
    Code (CSharp):
    1. void RandomString() {
    2.          int n = Random.Range(0, names.Length);
    3.          TextBox.GetComponent<Text>().text = names[n];
    4. }
     
  3. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    Thanks for your reply I'm still a little confused. where do i create the array? and what is the inspector
     
  4. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
  5. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
  6. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Did you do the Roll A Ball tutorial? It would give you the basics about how to use Unity and the scripting API. It doesn't need to download assets, although there is an Asset package. You don't need it, just follow the tutorial there: https://learn.unity.com/project/roll-a-ball-tutorial

    At some point, the instructor (@Adam-Buckner) mentions using Ctrl+' or Cmd+' to open the scripting API from Unity, these controls do not work with Visual Studio. The controls in Visual Studio are Ctrl+Alt+M and then Ctrl+H.
     
  7. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    ok thank you alot for your time and help i will be sure to look into it thanks
     
  8. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    i truly thankyou all for taking time to help me. this community is awesome