Other
UI Text Not Updating
I'm making a simple Arkanoid clone. When the ball goes below the paddle, I want "Game Over" to display in the middle of the screen. So when the game starts, I set the text to "" so there's nothing displaying during gameplay. Then, when the ball goes through the lower boundary trigger, I set the text to "Game Over". Only problem is, none of that works. I know there must be something really simply behind it. I thought I had a good handle on how to do UI Text at least, but apparently not :'D. Here's my code, and I've commented every section where I do something with text. I've included a couple screenshots from the editor at the end.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ball : MonoBehaviour
{
public float speed = 100.0f;
private new Rigidbody2D rigidbody;
private SoundControllerScript soundController;
public Text gameOverText; //text variable
void Start()
{
gameOverText.text = ""; //setting the text blank here
rigidbody = GetComponent<Rigidbody2D>();
rigidbody.velocity = Vector2.up * speed;
GameObject soundControllerObject = GameObject.FindWithTag("SoundController");
if (soundControllerObject != null)
{
soundController = soundControllerObject.GetComponent<SoundControllerScript>();
}
else
{
Debug.Log("Cannot find 'SoundController' object");
}
}
float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketWidth)
{
return (ballPos.x - racketPos.x) / racketWidth;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Racket")
{
//calculate hit factor
float x = hitFactor(transform.position,
collision.transform.position,
collision.collider.bounds.size.x);
//calculate direction, set length to 1
Vector2 direction = new Vector2(x, 1).normalized;
//set Velocity with direction * speed
rigidbody.velocity = direction * speed;
soundController.BallHitsRacketSound();
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "OutOfBoundsTrigger") {
gameOver.text = "Game Over"; //setting the text to Game Over here
soundController.BallHitsRacketSound();
}
}
}
Here is the Ball prefab showing that GameOverText has been dragged onto it.
And here's the hierarchy showing that both the Ball prefab and GameOverText prefab are in the scene:
Try this and see if it helps:
collision.transform.name == "OutOfBoundsTrigger"
Unfortunately I tried it and that change in code does not change anything.
I know that the trigger is already firing, because I also play a sound at the same time, as you can see in the code:
if (collision.gameObject.name == "OutOfBoundsTrigger") {
gameOverText.text = "Game Over";
soundController.BallHitsRacketSound(); //this bit plays the sound
}
The sound plays, but the game over text does not change on-screen.
If you put random text into it in the editor, does the Start() method successfully blank it?
I've discovered that if I unpack the Ball prefab, this works as it should. But why is that? And then is it simply not possible to have a changeable text UI object attached to a prefab through the inspector?
The only reason I would expect a difference in behaviour is if you have changes in the scene that have not been applied to the prefab. A prefab is just an GameObject stored in a separate file really.
Do you have changes that need to be applied?
Also, does the script successfully blank the text at Start()? This is a good test to make sure it is talking to the right component.
You're right. :p I've turned it back into a prefab now, and it still works. I don't know what could have been causing it, because I literally just clicked "unpack prefab" on the prefab Ball object in the scene, then clicked play immediately after and it worked. Thank you for your help on this and on my other questions. :)
Answer by xxmariofer · Apr 22, 2019 at 11:41 AM
i was reading your comments and you solved your own issue "And then is it simply not possible to have a changeable text UI object attached to a prefab through the inspector"
yes it is not posible, since the prefab isnt an instance in the scene, you would need to find it by tag/name in the awake/start, if you think about it will make a lot of sense, imagine you want to use your prefab now in scene 2, how is the prefab suppost to find the object in scene 1? you can never reference in a prefab ANY INSTANCE of your objects, but you can create an instance in the scene of that prefab and drag and drop the reference.
Follow this Question
Related Questions
The type or namespace function could not be found 1 Answer
Words in my script don't turn blue, why not? (absolute script noob) 3 Answers
GameObject.Find : why not working? 3 Answers
How to add information to an instantiated object 0 Answers
Merge Points collection system via a separate script 0 Answers