Check if a string is empty and fill it with something?
Hi! Im doing a memory game for kids and in the start menu I ask the players (up to four persons) to fill in their names using Text Mesh Pro input fields. This so that they can see their names on a scoreboard.
Brian: 1, Emma: 2, Carl: 5
Im suspecting that not everyone will fill in their names for different reasons. The problem is that the scoreboard looks bad. with nothing typed in its just :1, :2 :5.
So If they leave the input field blank and my strings (firstPlayerName, secondPlayerName etc) likewise - i would like to replace it with "Player 1", "Player 2" etc
I thought it was an easyfix with an if statement.
if (firstPlayerName == "")
{
firstPlayerName = "Player 1";
}
it didnt work so i tryied:
if (firstPlayerName == null)
{
firstPlayerName = "Player 1";
}
didnt work.
if (System.String.IsNullOrEmpty(firstPlayerName))
{
firstPlayerName = "Player 1";
}
nope
if (string.IsNullOrEmpty(firstPlayerName))
{
firstPlayerName = "Player 1";
}
it didnt work either. Now I am out of ideas and don´t know how to google the question differently. Please help!
Answer by marrl4killer · Jan 29, 2021 at 03:30 AM
First of all make an InputField...
private TMP_InputField player1Field;
Then don't directly check the .text, instead do this...
string player1Name = player1Field.text;
If ( string.IsNullOrWhiteSpace(player1Name))
{
\\insert code here
}
Answer by highpockets · Aug 26, 2020 at 11:40 PM
You are just changing a string here and each of your examples are valid. I think you may be missing passing that string to the actual text object. textObject.text = “Player 1”
, but I don’t know where you are storing your text object, but either way, you need to get a reference to the text object and pass the string to it
Ok, you just posted your comment as an answer, but in your code, I see that you are changing the empty string values in the Start() which is only happening before the first frame of the scene or before the first frame when the GameObject is instantiated. Do the players type their names before starting this scene? If so, is that data persisting Between the 2 scenes (are you using DontDestroyOnLoad()) on those text GameObjects?
If everything above was intended, another thing to look into is if there are any other scripts setting the text values??
**Also, I noticed a possible error in your code. Player1Name
seems to be the only member out of the 4 player names which starts with a Capital letter and the rest start with lower case.
The players type their name in a "$$anonymous$$ain$$anonymous$$enu"-scene and the namestring and other important values are saved into the next scene using DontDestroyOnLoad - whit an object called "Joined Players". This works because their typed names, amount of players etc follow along to the next scene.
Yeah, I saw the capital P earlier today. It didn't cause any problems but i changed it anyway according to c# protocol.
I've tied moving the problematic section into the update method but it didn't help: It should work in the start method because all the data are there from start, right? The Script i shared with you are not complete. I copy-paste the sections i belive was relevant for the issue. The complete script is kind of messy because i started this project half a year ago as my first Unityproject - following some tutorial i frankly didnt understood much of. Do you want to see the entire script(s)?
Answer by KalleManuel · Aug 27, 2020 at 08:48 AM
Their names are displayed at different places and in different part of the game.
I do pass the string to a textobject. Let me give you more of the script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using TMPro;
public class GameController : MonoBehaviour {
// Players turn & score
public GameObject playersInGame;
public int thePlayers;
public string firstPlayerName;
public string secondPlayerName;
public string thirdPlayerName;
public string forthPlayerName;
public TextMeshProUGUI Player1Score;
public TextMeshProUGUI Player2Score;
public TextMeshProUGUI Player3Score;
public TextMeshProUGUI Player4Score;
public GameObject scoreHolder1;
public GameObject scoreHolder2;
public GameObject scoreHolder3;
public GameObject scoreHolder4;
public int score1 = 0;
public int score2 = 0;
public int score3 = 0;
public int score4 = 0;
public int whosTurn = 1;
public GameObject scoreholderSolo;
public TextMeshProUGUI counter;
public TextMeshProUGUI score;
void Start()
{
// get player amount
playersInGame = GameObject.FindGameObjectWithTag("Players");
thePlayers = playersInGame.GetComponent<JoinedPlayers>().joinedPlayers;
firstPlayerName = playersInGame.GetComponent<JoinedPlayers>().Player1Name;
secondPlayerName = playersInGame.GetComponent<JoinedPlayers>().player2Name;
thirdPlayerName = playersInGame.GetComponent<JoinedPlayers>().player3Name;
forthPlayerName = playersInGame.GetComponent<JoinedPlayers>().player4Name;
gameLevel = playersInGame.GetComponent<JoinedPlayers>().level;
// replace empty names
if (firstPlayerName == "")
{
firstPlayerName = "Spelare 1";
}
if (secondPlayerName == "")
{
secondPlayerName = "Spelare 2";
}
if (thirdPlayerName == "")
{
thirdPlayerName = "Spelare 3";
}
if (forthPlayerName == "")
{
forthPlayerName = "Spelare 4";
}
private void Update()
{
// sologame scoretext
counter.text = firstPlayerName + ": " + guesses + " försök!";
// Player 1 scoretext
Player1Score.text = firstPlayerName + ": " + score1;
// player 2 scoretext
Player2Score.text = secondPlayerName + ": " + score2;
// player 3 scoretext
Player3Score.text = thirdPlayerName + ": " + score3;
// player 4 scoretext
Player4Score.text = forthPlayerName+ ": " + score4;
}
}
So the names the players type in do display. So the string > textobject works. But if they leave it blank nothing shows.