Want to Start Writing Code in Unity? Start Here! Unity C# Basics Part 1 - Writing Your First Script

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ก.ค. 2024
  • If you've been using Unity for a while, or are just getting started with Unity and decided it's time for you to start learning how to write code - this is the video you need to start your programming journey!
    In this tutorial, aimed at absolute beginners who have very little to no coding experience whatsoever, you'll learn:
    ⚫ Installing Visual Studio to write code
    ⚫ What is a "Script"?
    ⚫ Creating your first Script
    ⚫ What's going on with the default stuff in your new script?
    ⚫ A step-by-step breakdown of what each statement is doing, why it's happening, and how it works.
    ⚫ How to make numbers on the screen count up over time
    🚨 Resource to help you, as promised! First link 👉 docs.unity3d.com/ScriptRefere...
    💸 Ongoing sales 💸
    ⚫ See all active asset sales on the Asset Store: assetstore.unity.com/?on_sale...
    ⚫ Save 25% off your first Asset Store Order: prf.hn/click/camref:1101l9QvC...
    ⚫ Save up to 50% on NEW Assets: assetstore.unity.com/?new_sal...
    👨‍💻 As always, all code from this video is available on GitHub: github.com/llamacademy/unity-...
    ❤ Believe in LlamAcademy's mission and have received value from the videos? Become a Patreon Supporter or TH-cam Member:
    ⚫ Patreon: / llamacademy
    ⚫ TH-cam Member: / @llamacademy or click the Join button on any video
    Full Series Playlist Here: • Want to Start Writing ...
    ⚫ Part 1 - Writing your first script - • Want to Start Writing ...
    ⚫ Part 2 - Loops and Arrays - • Loops and Arrays | Uni...
    ⚫ Part 3 - Unity Component Lifecycle - • MonoBehaviour Lifecycl...
    ⚫ Part 4 - Static vs Instances - • Static vs Instances | ...
    ⚫ Part 5 - Delegates - • Delegates? Basically, ...
    ⚫ Part 6 - Inheritance & Interfaces - • Inheritance and Interf...
    ⚫ Part 7 - Compositional Relationships - • Compositional Relation...
    📝📝📝YOUR HOMEWORK!📝📝📝
    Create a new script that will show a random number between 0 and 1 and display that on a TextMeshProUGUI component, formatted to 2 decimal places.
    Add a comment below with a video showing your code and the end result.
    📚 For those looking to get ahead of the next video, consider some further reading on resources I brought up during this tutorial 📚
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ⚫ docs.microsoft.com/en-us/dotn...
    ----
    Most tutorials come from knowledge gained making survival.llama.software Llama Survival - a top-down zombie survival shooter for Android and iOS.
    I also have some Unity Assets (affiliate link): assetstore.unity.com/publishe...
    Some links may be affiliate links, which at no additional cost to you, gives me a small portion of the purchase.
    #unitytutorial #tutorialtuesday #gamedev #tutorial #unity #llamacademy #gamedevelopment #learntocode
    Chapters:
    00:00 What to Expect
    02:52 Ensuring You Have the Right Tools
    04:05 Setting Up the Scene
    04:36 Creating a Script
    05:34 First Look at Code - "using" Statements
    06:23 The Class
    07:30 Functions/Methods
    09:11 Class Member Variable Definitions
    16:20 The "if" Statement
    17:35 Assigning a New Value to a Variable
    20:50 Accessing Variables, Properties, and Functions on Another Class
    21:53 Calling a Function
    23:00 Converting Float Type to String Types
    24:45 Formatting a "Number" for Display
    25:58 Adding Your Custom Script to a GameObject
    26:24 Demo
    27:00 Closing Thoughts and Homework

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

  • @EverlongGames
    @EverlongGames 2 ปีที่แล้ว +13

    I started learning how to code more than half a year ago now, but still gave this video a watch since one can always learns new stuff... and I was right indeed! I didn't know that you could set the amout of decimals with the to string method, that's pretty cool! Also, this looks like a great start for beginners, I hope you continue with the series ;)

  • @spark3777
    @spark3777 11 หลายเดือนก่อน +6

    Absolutely love this tutorial. You explani every single element of what you are doing, and that is far better than every other tutorial where they explain the surface. Thanks!

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

    22:30 To clarify, arguments and parameters are not the same. A parameter defines what data the function will accept and an argument is data passed into a function.

  • @MM-bw1lo
    @MM-bw1lo 6 หลายเดือนก่อน +1

    Great info, I'm going through all these videos and learning a lot. Thanks so much for sharing.

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

    Oh also I just used your floating damage text in the game in working on. Very helpful.

  • @adelindanaila
    @adelindanaila 6 หลายเดือนก่อน +2

    Thank you! I had a little bit of experience with Unity and C# and I'm not completly new, to be honest. After a few months of Unity I took a long break since I was too tired and too little motivated to continue. Now the motivation of learning Game Dev is back but I'm kinda feeling overwhelmed. Your work is really helping me take it step by step again and regain the trust i had in myself when I first started feeling more confident with programming. Hehe! I went as far as adding a speed modifier slider for the homework since a new random value every frame is kinda hard to read. (with the help of Google, of course, since i didn't even know if i was able to have such thing as a slider). I'll keep a close eye on your content from now on!

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

    You helped me out on another one of your tutorials earlier today with regards to the RTS controller SHIFT CLICK debacle. Thanks for that, and I'm subbin' it up.

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

      Welcome! 💪👍🔥

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

    My savior. You made this at the right time! i just started doing unity and found this, thank you so much man! I'm really liking unity development so far, looking to become a developer.

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

      That’s awesome to hear! I hope the series helps you on your journey!

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

    perfect video for a friend of mine who wants to start learning unity :D

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

    It's great that you are putting stuff like this to help out beginners. A couple notes: I heard they are teaching it as "GEMDAS" now. G for group. Not sure how wide spread that is. Second, I know it's too early in this series for it, but I just learned how to do async/await in c#. There wasn't a lot of unity centric info on it. That would be a great topic eventually.

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

      Thanks for the info on GEMDAS. That's good to know!
      async/await is definitely a more advanced topic but I will add it to the list of topics to consider in this tutorial series!

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

    Thank you for your great work! I always wanted to do some code for my projects but the depth and the quality of my coding were always too shallow and weak. I hope through this series my coding ability and my understanding of programming gain some confidence and quality!
    And my code is:
    TimeDisplay = Random.Range(0.0f, 1.0f);
    Text.SetText(TimeDisplay.ToString("N3"));

  • @user-bv7xp8fy9t
    @user-bv7xp8fy9t 7 หลายเดือนก่อน +1

    cool video)

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

    My friend is so biased towards visual scripting i told him so many times to try coding and he doesn't want to cause he's afraid lol so maybe this video will change his mind thank you for taking the time and effort to put this one up kudos to you

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

      I hope so! I'm definitely a huge fan of the power of writing your own code. Visual Scripting definitely has a place, but also some limitations. I hope I can make this series well enough to make coding in Unity approachable for everyone

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

    I like this new format; I think a discord server would elevate it.
    Set up channels for homework and channels for homework help.

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

      Great idea! I will consider setting up a discord server

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

      @@LlamAcademy if you need an extra pair of hands, I should be able to assist if needed.

  • @fishngamer
    @fishngamer 2 หลายเดือนก่อน

    ty my sigma

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

    This looks like a good tutorial series and I appreciate the work you've put into it.
    That said, I've never used Unity before but I know C#. I thought this could be a good "Intro to Unity" type video since I'd like to get into programming right off the bat, however I'm a little lost at 4:05 "setting up the scene". It looks like you jumped ahead assuming the viewer already knows how to set up the scene the same way.
    I was able to figure out how to add a canvas and the TMP component, but I have no idea how you created the background. I'm sure it's really simple. Might be nice to just touch on the scene setup a little more in depth, or point the viewer to another video series.

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

      Thank you for that feedback. I started the series assuming the viewer did already have some background using Unity. I’ll keep that in mind going forward that maybe I should slow down and consider those viewers who have not already used Unity very much.

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

      You probably figured this out months ago. But for anybody struggling with Unity, and I'm one as well. What helped me a lot to learn how to do navigation and scenes, is that I'm doing the Lego Microgame tutorial. Now on the modding part and then can upload the game I modded. It's helped quite a bit. Only about a week and a half into Unity.

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

      how do i add the background?

    • @HoghPogh
      @HoghPogh 10 หลายเดือนก่อน

      You may have already figured it out but for those that come after, right click on canvas, hover over the ui option on the menu then click image(he names it background here) then when he clicks on the background tab pause the video and match your image settings (on the right) with his settings. @@Frogzilla1289

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

    Thank you for this. Just started "Create with code" tutorial in Unity. So far just did the translate Transform. New to Visual also. At 9:30 in this video I am not getting the drop down when I start to write the code in the area where code is entered. I have the screwdriver thing on the left and hovered over it and it shows prompt helpers I guess. Learned that Unity doesn't do ctrl z very well. Have had to redo Lego Microgame modding once so far. Messed the camera up on first try so it just points out and does not follow the minifig anymore. The prompt thing is important. How do I get it to do the drop down like yours ? Sorry to ask, but I don't know what it's called so can't even google to find the answer.

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

      In Visual Studio that's called "IntelliSense". One reason that might not show up is maybe in your Visual Studio installation you did not include the "Unity Tools". You can install those by double clicking your Visual Studio Installer again, clicking "Modify" on the version you have installed, and ensuring "Game Development with Unity" is selected. If that's installed for sure, you can check out this article that walks through some other reasons: madhawacperera.medium.com/how-to-enable-intellisense-on-visual-studio-when-working-with-unity-b07c27a5e47f

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

      @@LlamAcademy Wow, thank you so much for how to do that. At Unity. Seven months to get here. Started learning 3d design in Blender, to make a children's ABC game. Had the idea since 2001. I guess I move slow. Just couldn't make the character's personality. Just by accident I asked a designer on cgtrader to see if he could make a 3d model from a sketch I sent him. He made such a cute model that I now have to succeed in making the game. We are both excited to see the model come to life. Thanks again. Subscribed. You explain the concept behind the code. Was missing this. The Unity tutorial is ok, but skips a lot.

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

      @@LlamAcademy Update. Was able to follow the link instructions which worked. Another step on the Create with Code. It is so helpful for Intellisense to be working. The Unity tutorials are easy to follow, but leaving a step out to have the prompt thing may be intentional in order to get us as nebs to search out solutions. Idk. Thanks again for helping me on this.

  • @sebastianosewolf2367
    @sebastianosewolf2367 2 หลายเดือนก่อน

    this video is great but tbh what i got here dosent really help to create a new script for getting random numbers
    could give a hint or explain it ?

    • @LlamAcademy
      @LlamAcademy  2 หลายเดือนก่อน

      In the homework section I mention the first link in the description is to the Unity Documentation on how to generate random numbers. Check that out and you'll have all the information you need to complete the homework!

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

    04:05 my screen dosnt look the same as yours. i followed all the steps but i only have a light and a camera on my screen. what do i do???

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

      You can create the components I highlight immediately after that timestamp

    • @obsoletevalues6209
      @obsoletevalues6209 4 หลายเดือนก่อน

      @@LlamAcademy I have the same question. And the video after the 04:05 timestamp does not explain how to create EventSystem and Canvas (with its subordinate nodes). After researching, I found out I can select GameObject > UI > EventSystem, then do the same thing for Canvas. Then select Canvas in the Hierarchy, and select GameObject > UI > Text - TextMeshPro. I had to install it. I could not figure out how to create the background that was used in the video.

  • @LucRio448
    @LucRio448 4 วันที่ผ่านมา

    Okay so I neither can find a way to get a background, nor does the...middle window look the same, nor did it look the same for me at 26:15. When I opened the CountUp "menu" like at 26:15 for example, there is only the greyed out first line visible, the other two are not even there - should've mentioned that you absolutely have to save the script file before going back to Unity, I honestly thought Unity would like update the script live while by default. Also, sometimes you seem to click on stuff like offscreen, or is youtube being weird?

    • @LlamAcademy
      @LlamAcademy  4 วันที่ผ่านมา

      The "background" is simply a UI Image colored black. At 04:05 I quickly show these. I made this video from a context that you are familiar with Unity somewhat already, which I see from some of these comments, caused some confusion.
      The set up is just a Canvas created, then create a UI Image, color it black, and make it fill the full Canvas. Add a TextMeshProUGUI game object under the Canvas as well. Then you will have it set up the exact same as what I did.
      As for "doing things off screen", do you mean the part just after the timestamp you mentioned where I am dragging the TextMeshProUGUI reference to our "Count Up" script? I should have made the zoomed in window follow the mouse there.

  • @lylewyant3356
    @lylewyant3356 9 หลายเดือนก่อน

    Half the battle is knowing what questions to ask .

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

    I'm using code I thought it was gonna be hard now I'm actually doing it it is relly easy just type jump for jump run for run not hard

  • @rocketB343s
    @rocketB343s 28 วันที่ผ่านมา

    is it ok to follow this tutorial without text mesh pro? ,since i don't know how to use it

    • @LlamAcademy
      @LlamAcademy  27 วันที่ผ่านมา +1

      You can, but the way we interact with the text is not the same with TextMeshPro versus the standard text component. I highly recommend to use TextMeshPro instead of the standard text because it looks much nicer and has more functionality.

    • @rocketB343s
      @rocketB343s 27 วันที่ผ่านมา

      @@LlamAcademy K thanks

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

    My MonoBehaviour isnt green. Any help?

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

      Usually this means the project was not generated properly. You can try to close Unity and Visual Studio, open the project in your operating system and delete all the ".csproj" files, and reopen it in Unity. This makes Unity regenerate the files and usually fixes the problem

  • @sadpow2894
    @sadpow2894 4 หลายเดือนก่อน

    when i load up visual studio the script isnt showing? what am i doing wrong

    • @LlamAcademy
      @LlamAcademy  4 หลายเดือนก่อน +1

      If you double click the script from Unity Project Panel does it open correctly?

    • @sadpow2894
      @sadpow2894 4 หลายเดือนก่อน

      @@LlamAcademy it opens Microsoft visual studio but the area where the script would be is just completely blank

    • @TheTrolderia
      @TheTrolderia 3 หลายเดือนก่อน +1

      @@sadpow2894 I don't know why it happends, but it happends for me sometimes on my other pc. Then i needed to drag the script into the blank page of visual studio, and then it worked :)

    • @sadpow2894
      @sadpow2894 3 หลายเดือนก่อน +1

      @@TheTrolderia thank you:)

    • @TheTrolderia
      @TheTrolderia 3 หลายเดือนก่อน

      @@sadpow2894 Np man, gl on your journey! :)

  • @stefaniafiorucci9826
    @stefaniafiorucci9826 6 หลายเดือนก่อน

    How do I create the background?

    • @LlamAcademy
      @LlamAcademy  6 หลายเดือนก่อน

      The background in this case is just an Image stretched to the full size of the Canvas with just a solid color

    • @obsoletevalues6209
      @obsoletevalues6209 4 หลายเดือนก่อน

      @@LlamAcademy I have the same question. And the video after the 04:05 timestamp does not explain how to create EventSystem and Canvas (with its subordinate nodes). After researching, I found out I can select GameObject > UI > EventSystem, then do the same thing for Canvas. Then select Canvas in the Hierarchy, and select GameObject > UI > Text - TextMeshPro. I had to install it. I could not figure out how to create the background that was used in the video.

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

    I cant find visual studio 2019

  • @artthounasty5877
    @artthounasty5877 9 หลายเดือนก่อน

    I keep getting 6 or so decimals even though the "N2" is there... Does this no longer work or did I goof up?😅

    • @LlamAcademy
      @LlamAcademy  9 หลายเดือนก่อน

      Can you put the full line of code? It hasn’t changed. Maybe it was something small

    • @artthounasty5877
      @artthounasty5877 9 หลายเดือนก่อน

      @@LlamAcademy I don't have access to the file rn so I decided to just redo it since it's so short, and now it's working :)
      Thank you for being willing to help though!

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

    how do i add a background???

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

      shitty way to start a tutorial

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

      The background is just an Image set to color of black that I stretched to the full canvas size

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

      @@LlamAcademy sorry for saying the start was bad i was just mad

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

      @Frogzilla1289 I didn’t see that 😁. I hope you got the initial scene set up okay and can follow on from there.

    • @wheatieB54
      @wheatieB54 3 หลายเดือนก่อน

      still doesnt make sense one how to even add an image and or display the text of the timer@@LlamAcademy

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

    HW Video : th-cam.com/video/YvKlS96qSCM/w-d-xo.html
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    public class RandomNum : MonoBehaviour
    {
    [SerializeField] private TextMeshProUGUI text1;
    [SerializeField] private float minBoundry = 0f;
    [SerializeField] private float maxBoundry = 0f;
    private float ranNumber = 0f;
    // Update is called once per frame
    void Update()
    {
    ranNumber = Random.Range(minBoundry, maxBoundry);
    text1.SetText(ranNumber.ToString("N2"));
    }
    }
    Hope i did it right 😃

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

      🙌 great job! Remember we are looking for the number to be rounded to 3 decimal places though, in your code I see it will only give us to 2 decimal places!

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

    🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥Plz could u make a tps advance game tutorial for me🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥