I'm trying to create a falling word game like z-type (code here). I have created 3 game objects and I want the words to be added as text to these game objects something like a health bar is added to enemies etc. This is the word spawner script that spawns the word on screen.

public GameObject wordPrefab;
public Transform wordCanvas;

public WordDisplay SpawnWord()
{
Vector3 randomPosition = new Vector3(Random.Range(-2.0f, 2.0f), 7.0f, 0);
GameObject wordObj = Instantiate(wordPrefab,randomPosition, Quaternion.identity, wordCanvas);
WordDisplay wordDisplay = wordObj.GetComponent<WordDisplay>();
return wordDisplay;
}


Right now the words are spawned at a random x position and y position of 7.0. Instead of this how can I assign the words to the gameobjects that I have created.

Edit

I have created a spawner that spawns 3 enemies at the top of the screen and they start falling down to the bottom. Here is the script attached to the enemy game objects:

public float fallSpeed = 1.0f;
public GameObject wordPrefab;
private Transform parent;

public WordDisplay SpawnWord()
{
Vector3 randomPosition = new Vector3(Random.Range(-2.0f, 2.0f), 7.0f, 0);
GameObject wordObj = Instantiate(wordPrefab, randomPosition, Quaternion.identity, parent);
WordDisplay wordDisplay = wordObj.GetComponent<WordDisplay>();
return wordDisplay;
}

// Update is called once per frame
void Update () {
transform.Translate(0, -fallSpeed * Time.deltaTime, 0);
}


I have also removed the SpawnWord() method from the wordspawner script. However the words don't spawn along with the enemy gameobjects. There is one more script called the WordTimer. Not sure where to add this code. This is from the original code that's on github.

public WordManager wordManager;
public float delay = 1.5f;
private float nextWord = 0f;

void Update()
{
if (Time.time >= nextWord)
{
nextWord = Time.time + delay;
delay *= 0.95f;
}
}

• Surely instead of randomPosition just use wordCanvas.transform.position ? – JeffUK Jan 16 '19 at 13:16

instead of randomPosition just use wordCanvas.transform.position
• Easiest option would be to put the above script in the start() event of a script attached to each of the gameobjects, then parent them to transform. – JeffUK Jan 16 '19 at 13:49