Search Unity

Random Splash Help?

Discussion in 'Scripting' started by ShadoRadience, Jan 9, 2019.

  1. ShadoRadience

    ShadoRadience

    Joined:
    Jan 8, 2019
    Posts:
    2
    So, been messin around i know some c# and a decent understanding of unity, im currently trying to make a splash text that changes every time you go back to the title screen. I want it to pick a splash from a line in a .txt document and display that and if you leave the title screen and come back itll randomly pick another line from that .txt document. Kinda like how Minecrafts splash text changes but the problem is i dont have any idea how to go about this, can anyone offer me some assistance?
     
  2. ShadoRadience

    ShadoRadience

    Joined:
    Jan 8, 2019
    Posts:
    2
    i didnt have to do the unix line ending i dunno why but if i added it on it would also print \n onto it so i got rid of it and got it up and running, i dont suppose theres any chance you know how i could get different ones of these to have a certain % chance of appearing? I have one in my splashes i want to be a rare chance of showing up. This is currently what i have programmed for the splash logic.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SplashLogic : MonoBehaviour
    7. {
    8.     public Text onSplash;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         TextAsset splash = Resources.Load("Splash") as TextAsset;
    13.         string[] splitSplashes = splash.text.Split('\n');
    14.         string finalSplash = splitSplashes[Random.Range(0, splitSplashes.Length)];
    15.         print(onSplash.text = finalSplash);
    16.         Debug.Log(finalSplash);
    17.     }
    18. }
    19.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,741
    Quickest way is to list the common lines many times, and the rare ones rarely.

    Another way is to supply some kind of number, perhaps at the start of the line, and write code to parse it out.

    When you use the second approach, you would iterate and sum up all the different numbers to get their total, and pick a random number from zero to that maximum total, then start through the list again, subtracting each number until you reach zero, and which ever item you're on, bam, that's it.