Search Unity

I want to make more input fields with names and than make the names apear in text randomly

Discussion in 'Scripting' started by dicsiroland9, Jun 11, 2019.

  1. dicsiroland9

    dicsiroland9

    Joined:
    Jan 9, 2019
    Posts:
    3
    i made 1 player name apear in to my text but i don't know how to make player 2 / player 3 apear in to my text randomly
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    So what is your problem here? Making the names of Player 2 and appear, or make the player names random? What are you trying to achieve?
     
  3. dicsiroland9

    dicsiroland9

    Joined:
    Jan 9, 2019
    Posts:
    3
    I want to make a Truth or Dare game and i want to make it with as manny players as possibe and the challenges that they become are going to be like : "Player1, give out as many sips as red clothes you are wearing. Drink 3 sips if you don't have any" and by the next one i want for example Player2 to be in a different text
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    So what is your Problem? Can you Show us where in your code you are stuck?
     
  5. dicsiroland9

    dicsiroland9

    Joined:
    Jan 9, 2019
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.Linq;
    6. using UnityEngine.SceneManagement;
    7. public class GameManager : MonoBehaviour
    8. {
    9. public Question[] questions;
    10. private static List<Question> unansweredQuestions;
    11.  
    12. private Question currentQuestion;
    13. [SerializeField]
    14. private Text factText;
    15.  
    16. public static string playernamestr;
    17. public static string playernamestr2;
    18. public static string playernamestr3;
    19.  
    20.  
    21. [SerializeField]
    22. private float timeBetweenQuestions = 1f;
    23.  
    24. void Start()
    25. {
    26.  
    27. if (unansweredQuestions == null || unansweredQuestions.Count == 0 )
    28. {
    29. unansweredQuestions = questions.ToList<Question>();
    30. }
    31.  
    32. SetCurrentQuestion();
    33.  
    34.  
    35. }
    36.  
    37.  
    38. void SetCurrentQuestion()
    39. {
    40. int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
    41. currentQuestion = unansweredQuestions[randomQuestionIndex];
    42.  
    43. factText.text = currentQuestion.fact.Replace("$", playernamestr);
    44.  
    45. }
    46.  
    47. IEnumerator TransitionToNextQuestion()
    48. {
    49. unansweredQuestions.Remove(currentQuestion);
    50.  
    51. yield return new WaitForSeconds(timeBetweenQuestions);
    52.  
    53. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    54. }
    55.  
    56. public void ButtonNextQuestion ()
    57. {
    58. StartCoroutine(TransitionToNextQuestion());
    59. }
    60. }
    61.  
    62. using System.Collections;
    63. using System.Collections.Generic;
    64. using UnityEngine;
    65. using UnityEngine.UI;
    66. using UnityEngine.SceneManagement;
    67.  
    68. public class INputScript : MonoBehaviour
    69. {
    70.  
    71. public InputField playername;
    72. public InputField playername2;
    73. public InputField playername3;
    74.  
    75. // Start is called before the first frame update
    76. void Start()
    77. {
    78.  
    79. }
    80.  
    81. // Update is called once per frame
    82. void Update()
    83. {
    84.  
    85. }
    86.  
    87.  
    88. public void SaveButton()
    89. {
    90. GameManager.playernamestr = playername.text;
    91. GameManager.playernamestr2 = playername2.text;
    92. GameManager.playernamestr3 = playername3.text;
    93. Debug.Log("Palyer Name is:" + playername.text);
    94. Debug.Log("Player2 Name is:" + playername3.text);
    95. SceneManager.LoadScene("StarterScene");
    96. }
    97. }