Why is this method returning Unity.GameObject?

Hello all,

First of all sorry for the generic title, I’m really at a loss at how to describe the problem in a few words.

I have this method:

209    private List<string> GenerateRandomWords()
210    {
211    
212        // Generate a List<string> of random length.
213        int listLength = Random.Range(5, 20);
214        var randomWords = new List<string>(listLength);
215       
216        for(var i = 0; i < listLength; i++)
217        {
218            string word = "";
219            int wordLength = Random.Range(3, 15);
220    
221            // Generate a "word" of random length with random characters.
222            for(var j = 0; j < wordLength; j++)
223            {
224                word += Alphabet[Random.Range(0, Alphabet.Length - 1)]; // Alphabet is a char[] containing the alphabet letters, both lower and upper case. It doesn't contain a space!
225            }
226            Debug.Log("Word: " + word);
227        
228            randomWords.Add(word);
229       }
230       return randomWords;
231    } 

The output I’m expecting is something like

"Word: auhAdPIolrD" etc etc.

But what I’m getting is (note the spaces! Not sure if they’re in the message formatting or they’re coming from the ether, but from Alphabet they ain’t).

Word: k 1 (UnityEngine.GameObject)l 1 (UnityEngine.GameObject)a 1 (UnityEngine.GameObject)y 1 (UnityEngine.GameObject)m 1 (UnityEngine.GameObject)P (UnityEngine.GameObject)i 1 (UnityEngine.GameObject)i 1 (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
GameManager:GenerateRandomWords() (at Assets/Scripts/GameManager.cs:226)
GameManager:Update() (at Assets/Scripts/GameManager.cs:27)

I can’t spot any obvious or hidden faults, but there must be because all the words I get are like this. I can’t understand what is my error, is anyone able to clarify?

Cheers!

I copied your code into a class, guessed at the other bits and ran it. Worked as you designed :slight_smile:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Alphabet : MonoBehaviour {

    string baseString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] alphabet;
    const int numLetters = 52;

	// Use this for initialization
	void Start () {
	    alphabet = baseString.ToCharArray();
        GenerateRandomWords();
	}

    private List<string> GenerateRandomWords()
    {

        // Generate a List<string> of random length.
        int listLength = Random.Range(5, 20);
        List<string> randomWords = new List<string>(listLength);

        for (int i = 0; i < listLength; i++)
        {
            string word = "";
            int wordLength = Random.Range(3, 15);

            // Generate a "word" of random length with random characters.
            for (int j = 0; j < wordLength; j++)
            {
                word += alphabet[Random.Range(0, numLetters - 1)]; // alphabet is a char[] containing the alphabet letters, both lower and upper case. It doesn't contain a space!
            }
            Debug.Log("Word: " + word);

            randomWords.Add(word);
        }
        return randomWords;
    } 
}