HOW TO CREATE A GAME UI IN UNITY 🎮 | Create And Change Your UI In Unity | Unity Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ม.ค. 2025

ความคิดเห็น • 35

  • @khairolhazeeq5426
    @khairolhazeeq5426 3 ปีที่แล้ว +2

    Never expected you to create a Unity tutorial! You are truly multi talented!

  • @manuelmedina38
    @manuelmedina38 2 ปีที่แล้ว

    very useful information to call some things from other scripts into the one u are working on, thanks!!!

  • @Dani_Krossing
    @Dani_Krossing  3 ปีที่แล้ว +8

    (EDIT TO VIDEO)
    Someone pointed out to me that if you update your UI using the Update() method, then you risk garbage collection happening. Which basically means the UI gets updated unnecessarily each frame, even if no changes happens to the UI. 🙂 And this might impact your games performance if you have a lot of it going on.
    So here is a fix to make sure the UI only gets updated IF any changes to it has happened:
    👾SCOREMANAGER CHANGES👾
    public class ScoreManager : MonoBehaviour
    {
    private int scoreLast; // NEW FIELD THAT CHECKS IF SCORE HAS CHANGED 👈
    public TMP_Text textScore;
    public int score = 0; // CHANGE THE FIELD TYPE TO INT 👈
    // Start is called before the first frame update
    void Start()
    {
    textScore.text = score.ToString() + " FIREFLIES";
    }
    // Update is called once per frame
    void Update()
    {
    if(scoreLast != score) // IF STATEMENT THAT CHECKS IF SCORE HAS CHANGED👈
    {
    textScore.text = score.ToString() + " FIREFLIES";
    }
    }
    }
    👾FIREFLYCONTROLLER CHANGES👾
    public class FireflyController : MonoBehaviour
    {
    private ScoreManager scoreManager;
    private void Start()
    {
    scoreManager = GameObject.Find("Canvas").GetComponent();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (collision.tag == "Player")
    {
    scoreManager.score += 1; // REMOVE THE "f" SINCE IT IS NO LONGER A FLOAT 👈
    scoreManager.scoreLast = scoreManager.score; // UPDATES THE scoreLast FIELD 👈
    Destroy(gameObject);
    }
    }
    }

    • @billykilpatrick7283
      @billykilpatrick7283 2 ปีที่แล้ว

      Sorry for the late comment, but I was just wondering, wouldn't the scoreLast field need to be set to public rather than private, seeing as it has to be referenced by a separate script? I'm only very new to this so I have no idea if I've missed something haha

    • @UTube2K6
      @UTube2K6 2 ปีที่แล้ว

      you can also trigger a custom events to update UI, so you don't have to check scoreLast != score 60 times per sec in your update.

  • @vuongvytran4250
    @vuongvytran4250 ปีที่แล้ว

    your video is really great, it make our project for homework easy, thank you so much.

  • @akifgecir
    @akifgecir 2 ปีที่แล้ว

    Your videos are extremely helpful! Thank you!

  • @skayskeleton2980
    @skayskeleton2980 3 ปีที่แล้ว

    You are highly underrated my friend ...

  • @wingzero8390
    @wingzero8390 3 ปีที่แล้ว

    Are u going to continue the c# series?.. i just finished them and i learned so much so quickly but I m now sad knowing that you haven't done any more videos.

  • @juliaalder2007
    @juliaalder2007 3 ปีที่แล้ว

    Thanks. I like your explanations. :)

  • @tinacao7703
    @tinacao7703 2 ปีที่แล้ว

    this was so helpful!!!!!!!!!!!!!!!!

  • @m4rlk4rx
    @m4rlk4rx 2 ปีที่แล้ว

    I have a question about the score variable, doesn't it also work if I use getters and setters instead of making the variable public?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Yes it does. In fact, if you want to be technically correct, you should ALWAYS use get and set, instead of changing or accessing fields directly. 🙂

    • @m4rlk4rx
      @m4rlk4rx 2 ปีที่แล้ว

      Ok thx thats realy helpful

  • @MaximumLowBlow
    @MaximumLowBlow 2 ปีที่แล้ว

    When I pick up my score object, it sometimes gets collected multiple times. Any idea why this might be? There's no duplicate object behind it, there's only 1 rigidbody/collider on my player.

    • @jigarpanchal0
      @jigarpanchal0 ปีที่แล้ว +1

      disable the collider onCollider/Trigger while you count the score

  • @karelbrouwer7840
    @karelbrouwer7840 ปีที่แล้ว

    I can't drag the script into the canvas. NEED HELP PLS

  • @hellovictor96
    @hellovictor96 2 ปีที่แล้ว

    For me it says "Object refrence not set to an instance of an object" how do I fix this? I did what you did in the video but got this messege when trying to collect my "fireflys" :)

    • @LilBlucky
      @LilBlucky 2 ปีที่แล้ว

      mine does too, did u find a solution?

    • @hellovictor96
      @hellovictor96 2 ปีที่แล้ว

      @@LilBlucky sorry i dont remember 😅

    • @ManishSingh-dj3ds
      @ManishSingh-dj3ds 2 ปีที่แล้ว

      it's because you have to drag and drop the textpro element to the script attacted to your game.

  • @dylanc5636
    @dylanc5636 2 ปีที่แล้ว

    Probably a long shot but if anyone knows the answer lol - sometimes when my player collides with the coin (I just used a coin instead of fireflies), instead of adding +1 it adds +2. Is this because it's detecting multiple collisions for some reason? If so, how do I prevent that? Thanks!

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Can you add the code here? 🙂

    • @dylanc5636
      @dylanc5636 2 ปีที่แล้ว

      @@Dani_Krossing Oh hey thanks for replying!! Here's the code, I've been following along with this tutorial so as far as I'm aware everything should be the same from the past videos in the series. It's been working well other than this small issue! If you need to see code from one of the other scripts let me know.
      public class CoinController : MonoBehaviour
      {
      private ScoreManager scoreManager;
      private void Start()
      {
      scoreManager = GameObject.Find("Canvas").GetComponent();
      }
      private void OnTriggerEnter2D(Collider2D collision)
      {

      if(collision.tag == "Player")
      {
      scoreManager.score += 1f;
      Destroy(gameObject);
      }
      }
      }

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Nothing looks wrong in this script 🙂 You said "sometimes" when your player collides with a coin... So not every time? 🙂 If this is the case then it sounds like the error is caused in Unity, and not in your scripts.
      For example by accidentally placing two coins behind each other in the scene, or accidentally placing two scripts on the coin object?
      I don't think ScoreManager would have any code that could cause this error, but if you double checked Unity to make sure the issue isn't there, then you are more than welcome to send me the ScoreManager class too so I can check if the error is in there. 😉

    • @dylanc5636
      @dylanc5636 2 ปีที่แล้ว

      @@Dani_Krossing Ooh interesting, I'll check to see if maybe I doubled up on a coin placement by mistake. It doesn't happen every time with the same coin which makes me think that may not be the issue, but I'll do some digging. Thanks so much for your help I really appreciate it!😄

    • @dylanc5636
      @dylanc5636 2 ปีที่แล้ว

      @@Dani_Krossing I did a little bit of messing around - there's no doubled up coins but I think it may have something to do with the colliders on the player? I'm just using the original rectangle as the player as this was more just for practice, so I have a capsule collider on the body and then the very small box collider at the bottom which is set to be a trigger. When I moved around the box collider ever so slightly, lowering it a bit, I ended up getting the +2 on coins more often! I also noticed that when I collide with the coin horizontally, so only the capsule collider is hitting it, it gives the +1 correctly. When I jump onto the coin, so the box collider/trigger AND the capsule collider both hit the coin, that's when I get the +2.
      I know it's almost impossible to diagnose a problem without actually seeing it but just figured I'd share what I noticed. I may go back to the older videos and double check I set up the player controller script correctly too. Thanks again!

  • @thedisguisedcat1465
    @thedisguisedcat1465 3 ปีที่แล้ว +1

    first

  • @bridgebridge3840
    @bridgebridge3840 3 ปีที่แล้ว +6

    Why do this guy look like Elon Musk

    • @lisali6205
      @lisali6205 3 ปีที่แล้ว

      become a little fat ?

    • @vampbat
      @vampbat 2 ปีที่แล้ว +1

      statistically there r 7 people in the world who look like you.. @ everyone..

  • @pranavtiwari_yt
    @pranavtiwari_yt 3 ปีที่แล้ว +2

    my useless comment for engagement

  • @davids2551
    @davids2551 2 ปีที่แล้ว

    Absolute Garbage. Using Unity 2021.3.11f1 and the text can't be dragged on to the public object in the script.

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว +1

      You were most likely using a regular font, instead of a TMP asset. Like I mentioned in the video, I show how to convert fonts into a TMP asset at the end. The timestamp shows where in the video timeline.

    • @davids2551
      @davids2551 2 ปีที่แล้ว

      @@Dani_Krossing I owe you an apology and a retraction from a total newb.
      For the benefit of any future newbs:
      Forget about your current level/scene when setting up TMPs, line up the TMP according to the large canvas frame that represents your monitor boundary.
      If the current part of the scene you're working on is a tiny piece in the bottom corner of the canvas and you line up the TMP over that then you won't see it when you hit play and everything gets scaled up to fill the canvas because the TMP will be left where it is in the wrong place.