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. Dismiss Notice

Unity Android Game Scripting

Discussion in 'Scripting' started by macoydolem, Aug 5, 2017.

  1. macoydolem

    macoydolem

    Joined:
    Aug 4, 2017
    Posts:
    5
    I want to know if a script can call multiple text element. I want a script to give random numbers to three text. (I can do the random stuff) I dont know how to manipulate multiple element in a script.
     
  2. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Is this what you mean?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI; //Add this!
    3. using System.Collections;
    4. using System.Collections.Generic; //Add this!
    5.  
    6. public class TextDisplayController : MonoBehaviour {
    7.  
    8.     public Text textDisplay; //using UnityEngine.UI; in order to use UI: like 'Text', 'Slider', and more...
    9.     public List<string> textList; //using System.Collections.Generic; in order to use 'List<> ()'...
    10.     private int randomNumber;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         GetRandomText ();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Input.GetKeyDown (KeyCode.Space))
    20.             GetRandomText ();
    21.     }
    22.  
    23.     void GetRandomText ()
    24.     {
    25.         randomNumber = Random.Range (0, textList.Count);
    26.         Debug.Log ("Text List:" + (randomNumber + 1) + " / " + textList.Count);
    27.  
    28.         if (textList.Count != 0)
    29.             textDisplay.text = textList [randomNumber].ToString ();
    30.     }
    31. }
    32.  
     

    Attached Files:

    Last edited: Aug 5, 2017
    macoydolem likes this.
  3. macoydolem

    macoydolem

    Joined:
    Aug 4, 2017
    Posts:
    5
    Oh im sorry. I actually found something after some digging.
    Capture.PNG

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ChangeN1 : MonoBehaviour {
    7.     [SerializeField]
    8.     public Text txtN1 = null;
    9.     public Text txtN2 = null;
    10.     public Text txtA1 = null;
    11.     public Text txtA2 = null;
    12.     public Text txtA3 = null;
    13.     public int n1, n2, nn, nHolder, nnn;
    Anyways. Thanks for the help. :)