Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random Generator Script Error?

Discussion in 'Scripting' started by Alishakitty28, Aug 23, 2019.

  1. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi,
    I'm making a random generator in my game but my script isn't working does anyone have any idea why?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace demo
    6. {
    7.     class Program { }
    8.  
    9.         static void main(string[] args)
    10.     {
    11.         var random = new random();
    12.         var list = new list<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
    13.         int index = random.Next(list.count);
    14.         console.WriteLine(list[index]);
    15.     }
    16. }
    Its probably really simple but I haven't been learning for that long and have only watched tutorials as a guide. Once the script is working I plan to change the numbers to different strings but I just wanted to keep it simple for now until its working.

    Thanks :)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    I assume you're watching a tutorial for some more generic kind of C# app, because that code is going to be useless in Unity. Unity scripts don't use the "main" method. In Unity, people also generally don't use C#'s Random class; Unity has its own Random class that is better optimized for games.
    Even besides all that I don't think that would work or even compile, because main() is outside of any class. Also List<> and Random are lowercase in your script, and capitalization matters. In the future, please copy and paste the actual errors you're getting, as it will make it much easier to find issues.

    See here for some sample code of how to do randomness in Unity.
     
  3. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi thanks, yes I was using pretty generic tutorials. Would I be able to change the script in your link to work for strings? Basically what I'm trying to achieve is when a button in pressed on screen the program randomly picks a sentence from list to form a story
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Something like this:
    Code (csharp):
    1. var list = new List<string>{"1", "2", "3"};
    2. int randomIndex = Random.Range(0, list.Count);
    3. Debug.Log(list[randomIndex]);
     
  5. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi, sorry, at this point it must but really obvious that I haven't had much experience with the program, I entered the code from your post
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace test
    6. { class random tester
    7.         {
    8.     var list = new List<string> { "1", "2", "3", "4", "5" };
    9.     int randomIndex = Random.Range(0, list.count);
    10.     debug.log(List[randomIndex]); }
    11.  
    I'm still getting errors and not being familiar with the coding side of things I can't figure out why it isn't working
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Start with a new script that Unity generates (you've made a bunch of changes to where it's easiest to just start over), then put my code inside the Start() { } function.
     
  7. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Ok will do
     
  8. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    That works perfectly now thank you, one last question how would I get the generated string to appear in my main text block? Sorry for taking up your time on probably really basic questions
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You add a UI canvas, and a GameObject under that, with a UI Text component attached. You put a reference to that component in the script you want to set the text from. You actually set the value of the text being output by accessing its text field.

    Code (csharp):
    1. public Text TextComponent;
    2.  
    3. void Start()
    4. {
    5.     TextComponent.text = "Something blah blah";
    6. }
    The above linked tutorial probably explains it better.
     
  11. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Admittedly I'm still really confused, I have the random generation for my strings now and I have all the buttons and text boxes in place but now what I need is for the answer of the random generation to appear in my text box when my button is clicked. (I know how to get it on click I just don't understand the code)