Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 22, 2019 at 12:10 PM by JayDeeCW for the following reason:

Other

avatar image
0
Question by JayDeeCW · Apr 21, 2019 at 04:15 PM · scripting problemuitextscripting beginnerscriptingbasics

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.

alt text

And here's the hierarchy showing that both the Ball prefab and GameOverText prefab are in the scene:

alt text

capture.png (33.0 kB)
capture3.png (13.8 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KevRev · Apr 21, 2019 at 05:41 PM 1
Share

Try this and see if it helps:

 collision.transform.name == "OutOfBoundsTrigger"
avatar image JayDeeCW KevRev · Apr 21, 2019 at 06:52 PM 0
Share

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.

avatar image KevRev JayDeeCW · Apr 21, 2019 at 07:43 PM 1
Share

If you put random text into it in the editor, does the Start() method successfully blank it?

avatar image JayDeeCW KevRev · Apr 21, 2019 at 07:42 PM 0
Share

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?

avatar image KevRev · Apr 21, 2019 at 07:52 PM 1
Share

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.

avatar image JayDeeCW KevRev · Apr 22, 2019 at 08:54 AM 0
Share

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. :)

1 Reply

  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

305 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges