How to get continuous reference to a GameObject's position?
Say, its a 2D word falling typing game, the words are individual GameObjects. How do I check the y position of each word GameObject falling down the scene and then trigger the Game Over text when it passes a certain y position?
Word GameObject Prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Word {
public string word;
private int typeIndex;
WordDisplay display;
public Word (string _word, WordDisplay _display)
{
word = _word;
typeIndex = 0;
display = _display;
display.SetWord(word);
}
public char GetNextLetter ()
{
return word[typeIndex];
}
public void TypeLetter ()
{
typeIndex++;
display.RemoveLetter();
}
public bool WordTyped ()
{
bool wordTyped = (typeIndex >= word.Length);
if (wordTyped)
{
display.RemoveWord();
}
return wordTyped;
}
void Update()
{
if(Transform.position.y == -328.1865)
{
GameOver.turnon = true;
}
}
}
Event script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOver : MonoBehaviour
{
public Text gameOverText;
public static bool turnon;
// Start is called before the first frame update
void Start()
{
gameOverText = GetComponent<Text>();
gameOverText.gameObject.SetActive(false);
turnon = false;
}
void Update()
{
if (turnon == true)
{
gameOverText.gameObject.SetActive(true);
}
}
}
The game is based on Brackey's Typing Game found here
Answer by tormentoarmagedoom · May 06, 2019 at 11:37 AM
HEllo.
I did not read the code, but for your question, I think is better to use a collider to detect when a word reaches a "zone" to be deleted.
Also for your question i see you are pretty new at Unity/scripiting. I strongly recommend to spend 4-5 hours waching some basic tutorial about colliders, about gameobjects, etc...
Good luck!
Answer by Hellium · May 06, 2019 at 12:42 PM
// Word.cs
void Update()
{
if(display.transform.position.y <= -328.1865f )
{
GameOver.turnon = true;
}
}
// WordManager.cs
void Update()
{
foreach( Word word in words)
word.Update();
}
Your answer
Follow this Question
Related Questions
instantiated gameobject position is not working 0 Answers
How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer
When hitting border teleport bullet to player. 1 Answer
Cannot properly access player position 1 Answer
How to get the position of a gameObject based on its index in an array? 0 Answers