Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MadScyan · Jul 10, 2016 at 11:17 PM · arraydebug.logprintchar

Cannot print an array of char to the console. It prints system.char[] instead of the actual letters

So basically im doing a hangman game printing the results directly on the console. Ive got everything working, last step is printing my array variable m_WordToDisplay which contains all of the guessed letters with the underscores for the spaces not guessed yet.

Every time the user makes a correct guess, i want to debug.log or print m_WordToDisplay, but when i do so, what it reads on the console instead of the actual word (ie. w__d), instead it reads System.Char[]

I know my variable m_WordToDisplay has the word stored in it because when i step into the code i see the guessed letter and the underscores. so the problem seems to really be just when printing the actual result. Here is my code which is relevant:

     private char[] m_WordToDisplay;

My initialize method where i choose the word and then fill in the array with underscores:

     public void InitializeGame (Player player, int numberOfMistakesAllowed = 6){
         if (player == null){
             Debug.Log ("ma player is null");
         }
         if (m_numberOfMistakesAllowed < 0){
             Debug.Log ("number of mistakes must be non negative");
         }
         m_Player = player;
 
         LoadWordList();
 
         m_numberOfMistakesAllowed = numberOfMistakesAllowed;
         Outcome = Outcome.InProgress;
         Word = ChooseWord ();
         m_WordToDisplay = new char [this.Word.Length];
         for (int i = 0; i < m_WordToDisplay.Length; i++){
             m_WordToDisplay[i] = ('_');
         }

My guess method to pass in the letter chosen by the user:

     public void Guess (char letter){
         if (Outcome != Outcome.InProgress)
             Debug.Log ("Game is not in progress");
 
         if (m_GuessedAlready.Contains(letter)){
             print("You already guessed that letter, dummy");
             return;
         }
 
         m_GuessedAlready.Add(letter);
 
         if (!Word.Contains(letter)){
             print("There is no" + letter);
             m_IncorrectGuesses += 1;
 
             if (m_IncorrectGuesses > m_numberOfMistakesAllowed){
                 Outcome = Outcome.Loss;
                 print ("You lost");    
             }
             else{
                 print ("Number of guesses left: " + MistakesRemaining());
             }
 
         }
         else{
             FillInCorrectGuess(letter);
             Debug.Log (m_WordToDisplay);  // THIS HERE DOESNT PRINT THE WORD WITH THE GUESSED LETTERS BUT INSTEAD System.Char[]
         }

and finally, here is my method where i fill in every letter in the word with the guessed letter when its a letter that is part of the word

     private void FillInCorrectGuess (char letter){
         for (int i = 0; i < Word.Length; i++){
             if (Word[i] == letter)
                 m_WordToDisplay[i] = letter;
             
         }

So yeah, all i want to do ideally would be something like
Debug.Log ("Good guess! " + m_WordToDisplay); But doing so prints "Good guess! System.Char[]"

Thanks in advance anyone

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ulci · Jul 11, 2016 at 01:09 AM

 new string(m_WordToDisplay)
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 MadScyan · Jul 11, 2016 at 02:33 AM

i feel like such a noob.

I actually tried a couple of stuff like m_WordToDisplay.tostring etc but never got around to do that.

Thanks for the tip!!!

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

65 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

Related Questions

Wher is the problem? My string isn't behaving... 1 Answer

How to print float items added inside a list 1 Answer

How do I convert a char array to string (C#)? 1 Answer

collision.contacts not working, 0 Answers

Finding the shortest path between nodes not working for enemy AI 1 Answer


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