0
\$\begingroup\$

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)
    {
        wordManager.AddWord();
        nextWord = Time.time + delay;
        delay *= 0.95f;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Surely instead of randomPosition just use wordCanvas.transform.position ? \$\endgroup\$ – JeffUK Jan 16 at 13:16
0
\$\begingroup\$

instead of randomPosition just use wordCanvas.transform.position

As you set the wordCanvas as the parent it will be parented to the wordCanvas and follow it around.

If wordCanvas isn't the object you're supposed to be following, substitute wordCanvas with that object.

\$\endgroup\$
  • \$\begingroup\$ thanks for the help. but how do I assign them to different game objects. i have two cubes and a sphere right now spawning at different position. so how do I assign a separate word to each game object? \$\endgroup\$ – single arrow games Jan 16 at 13:46
  • \$\begingroup\$ 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. \$\endgroup\$ – JeffUK Jan 16 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.