I am trying to make a game, and I need to make TextMeshPro text saying “Gameover” appear when a prefab is too far to the right or left. I have gotten code that will make a game object disappear when the prefab is too far but I can’t get the prefab to get the text. I have tried gameobject.find, I have having it as a public gameobject variablle, but I couldn’t drag it, or anything, in.
You can likely just find the gameover object based on a tag, just make sure the tag matches the code since it is case-sensitive. This isn’t the best way to do what you need, perhaps, but if it’s just a small game you should be fine.
[SerializeField] GameObject gameOverText;
void Start(){
gameOverText = GameObject.FindWithTag("GameoverText");
}
void Update()
{
if(transform.position.z > 2 || transform.position.z < -2)
{
gameOverText.SetActive(true);
}
}
This can be simple:
First, in the script that can make that gameobject disappear, also make the TextMeshPro gameobject appear. In that same class, you can create a field
[SerializeField] Gameobject gameOverText;
Then drag in your textmeshpro object in the Inspectory, then set it to disabled by default. When you make your gameobject disappear in the script, turn on the gameOverText object.
gameOverText.SetActive(true)
@rysan007 (I accidentally put disappear instead of appear but that doesn’t change the code too much) My problem is that I’m not able to put a gameobject from the hierarchy into the prefab’s fill-in box in the inspector because prefab’s can’t have an object from the scene always be in them because they can be out of the scene, I just couldn’t word it well at the time. This method still has the problem of not being able to put the gameover text into the prefab’s variable box.
[SerializeField] GameObject gameOverText;
// Update is called once per frame
void Update()
{
//make gameover text appear when too far to the sides
if(transform.position.z > 2 || transform.position.z < -2)
{
gameOverText.SetActive(true);
}
}