banner



How To Dynamically Update Script On Prefab Ui Objects In Unity

Unity 3D: Update UI Elements on Canvass in Realtime

In the terminal tutorial we learned how nosotros tin add a Text label UI Element to our Game sail.

We could have easily done that by just importing a sprite, right? Why did we bother making a Canvas? One of the major reasons is because we want to have the option of beingness able to modify the text, counters and images using a script. In fact, let's attempt that out!

Articulate the Text Field from the Text gameObject, so that information technology doesn't say anything.

Text UI element with empty text field

Create a new script chosen ScoreBehavior and open up information technology up.

          using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;  public course ScoreBehaviour : MonoBehaviour {     private Text thisText;     individual int score;          void Start()     {         thisText = GetComponent<Text>();                  // ready score value to be zero         score = 0;     }          void Update()      {         // When P is hit         if(Input.GetKeyDown(KeyCode.P))         {             // add together 500 points to score             score += 500;         }         // update text of Text element         thisText.text = "Score is " + score;     }  }        

Annotation: Line number 4 is quite an important lawmaking line hither. Your IDE will requite you lot a Missing Reference mistake if you lot forget to include whatsoever resource/course/package that you are using in your code, using the using directive.

So what else are we doing hither? First of all, we're initializing a Text grade variable with the name thisText. The Text grade is bachelor only if y'all add the using UnityEngine.UI at the meridian, since this grade is part of UnityEngine.UI package.

In the Showtime() function, you'll see recognizable statements, nil out of the ordinary. The first statement, assigns the thisText variable to the Text component attached to the gameObject, and the other simply sets the score to 0 initially.

In the Update() method, we define an input command to add 500 points to the score each time we press the P central.

Now, the Text course has a text property, which is stored as a string, and this value is what that instance will actually testify in the game. In fact, yous're setting the Text.text holding to a value each time you lot type in something in the text field in the Inspector view. What we're doing is updating the text value of our thisText object in each frame.

Save this script, and head back to Unity. The label seems to have disappeared, but when y'all run the game.

Adding UI Elements

Press P a few times, and y'all should see the score update!

Adding UI Elements


Updating the Text UI Chemical element from other scripts

You can extend this concept past adding a public static method to your script, and calling them from other Behaviour scripts.


What is static keyword for?

The keyword static makes a method or variable maintain a country till the end. It can be called in other classes by using the form name in which the static method is defined.

As well, only static variables tin exist used inside a static method.

This is generally a universal concept which stands true in example of Java, C# etc programming languages.

Our grade ScoreBehaviour will look like:

          using Arrangement.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;  public form ScoreBehaviour : MonoBehaviour {     private Text thisText;     private static int score;          void Commencement()     {         thisText = GetComponent<Text>();                  // prepare score value to be zero         score = 0;     }          void Update()      {         // update text of Text element         thisText.text = "Score is " + score;     }          // adding a new method to our grade     public static void AddScore()     {         // add 500 points to score         score += 500;     }  }        

If you call up, in the tutorial where we captured the collision of fireball with the target, we defined a class TargetBehaviour which had a OnCollisionEnter2D method defined. Let's update that method to update the score whenever the bullet hits the target.

          public class TargetBehaviour : MonoBehaviour {          void onCollisionEnter2D(Collision2D col)      {         // When target is hit         if(col.gameObject.tag == "Bullet")         {             Debug.Log("Target was Hit!");                          // calling AddScore method             ScoreBehaviour.AddScore();                          Destroy(col.gameObject);             Destroy(gameObject);         }     }  }        

This code will at present modify the score by 500 each time you hit the target. Create a few duplicates of the target gameObject and endeavour it out.

Updating the Text UI Element from other scripts

Updating the Text UI Element from other scripts

Wonderful. Of course, there is much more to the UI system than merely making static or dynamic text. You can add images, sliders, buttons and a lot of familiar UI elements by using this method. We urge y'all to try and explore these elements and figure out how they work, both in the Editor and in scripting.



Source: https://www.studytonight.com/game-development-in-2D/update-ui-element-in-realtime-unity

Posted by: olmoinlyrib.blogspot.com

0 Response to "How To Dynamically Update Script On Prefab Ui Objects In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel