Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Text on button click command

Discussion in 'Getting Started' started by Alishakitty28, Aug 26, 2019.

  1. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi, I posted this thread in a different area at first because I thought it was appropriate in that area, but now having seen this section I think things will be a little bit more beginner friendly for me.

    Basically what I want my game to be is a random generator life simulator similar to games like Instlife and Bitlife.

    I've got my layout sorted but I don't understand the coding much at all, I've seen a lot of tutorials on YouTube and unity and it hasn't really gotten me any further then altering the aesthetics of my UI (i.e changing colours etc)

    I think have my random generation code but I don't understand how to make the results of the random generation appear in my text box on screen. (obviously I would attach the finished script to a button)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,440
    private Text myText;

    myText = GameObject.Find("MyTextComponent").GetComponent<Text>();
    myText.text = myRandomNumber.ToString();
     
  3. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi, I already have this code written out but it says there are errors and I don't understand why

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonScript : MonoBehaviour
    6. {
    7.     public Text MainText;
    8.     public Button button;
    9.     public bool ButtonClick;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         MainText = "";
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (ButtonClick = true) ;
    22.  
    23.         { var list = new List<string> { "1", "2", "3" };
    24.             int randomIndex = Random.Range(0, list.Count);
    25.             Debug.Log(list[randomIndex]); }
    26.         Writeline = LinkedList; {in maintext};
    27.      
    28.     }
    29. }
    30.  
    Please not the numbers in the list will not be numbers in the finished project, I have only used numbers for simplicity for testing.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,440
    I believe you need MainText.text = "";

    Also, please post your errors. And your syntax looks to be incorrect, I'm not familiar with {in maintext} and a few other portions of your code.
     
  5. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    I have absolutely no idea what to do with it in all honestly, half the code is from people here and the other portion in from tutorials online. All I want to do is when my button is clicked have my program randomly pick a string from a list and put it in my main text box on screen
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,440
    You would want to replace your code with the code that I suggested. It would get rid of one of your errors. But honestly, you would want to take the advice on your other post (and please don't multipost) and learn C# outside of Unity first.
     
    Joe-Censored likes this.