Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KalleManuel · Aug 26, 2020 at 11:31 PM · stringinputfieldempty

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · Aug 27, 2020 at 09:08 AM 0
Share

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.

avatar image KalleManuel · Aug 27, 2020 at 10:15 AM 0
Share

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)?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting string to 'entire' contents of input field? 0 Answers

Check if 2 InputField contains 2 words (not the same)? Feel like it's easy but can't figure out what's wrong 0 Answers

Trying to compare an input field with an int 0 Answers

How to detect non-latin letters? 1 Answer

received string from inputfield appearing as null from other scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges