[Video][Game Making Tutorial 3] Adding a Point System + Coding Basics II

 

 

PickupsForTutorial

Pickups For Tutorial

Scripts and New Sprites

 

As you follow the video, you’ll find some new definitions, which are:

 

  1. Circle Collider 2D: Acts the same as a Box Collider, but with a different form to it.
    1. Is Trigger: Activating this checkbox will make our collider “intangible”, but still there. The game will know when an object enters the area, but won’t stop it from doing so. This can be used to many effects, such as opening a door, picking an item up, changing something from afar.

 

  1. New Public Int Variable: Points: This variable was added so other scripts can ‘read’ and edit it. That’s why it’s a public variable, too. Also, the public part will let you set an amout of points for each object with this script attached, as shown in the video.

 

  1. FixedUpdate: We must use this instead of Update for Physics Simulation (Adding forces to the player counts). The reasoning behind this is that Unity is updating Fixed Update with every Physics interaction, instead of every frame. It’s more reliable when there are more interactions on screen or at a given time.

 

  1. OnTriggerEnter2D: This script must be created and attached to an object whose Collider2D (Box, Circle, any) is set to “Is Trigger” in order to work. It detects objects that enter the Collider’s area and returns all the information about them. Usually, it’s used as:

 

  • void OnTriggerEnter2D (Collider2D COLLIDERNAME) {Instructions} ; where you can change the COLLIDERNAME to anything you want, usually 3-5 letter words.

 

  • Inside the Instructions, you can do many things. Usually, you have a kind of object you want to affect this. To check what kind of object entered the Trigger, you can use any of these:

 

  1. If (COLLIDERNAME.CompareTag(“TagName”)){ Instructions } will check if the Tag of the object (Set on the inspector, top left corner) is the same as the one in parenthesis.

 

  1. If (COLLIDERNAME.name == “Name”){ Instructions} will check if the name of the object that entered is the one in quotes.

 

  • You can combine your conditions. For example, to check if the Player character is IN THE AREA and the player INPUTS THE SPACE BUTTON, you can do:

 

  1. If (COLLIDERNAME.CompareTag(“Player”) & Input.GetKeyDown(KeyCode.Space)){Instructions} , Keycode plus the period will activate a list of keys for you to pick. GetKey DOWN means “When the Player Inputs a Button for the first time”.

 

  1. Whenever you want to access a script from another script, you can call a Find to look for that object. In most cases, you want to check and alter the Player or the LevelManager scripts (more on this in a bit). To find an object that is in your Scene only ONCE, you may do:
    1. On the Variable area call the Script Name and assign it a new name. For example, our Script called “Player”… we would do Player player;
    2. Next, in Start, find it. In this case: player = FindObjectOfType<Player>();
    3. Now, anytime you call “player”, you can use the period powers to summon its variables and functions. It’s that easy.

 

  1. Creating a UI object will create a Canvas Object. Canvases are representations of the game screen in any window, you can set Anchors to place objects at a specific spot no matter what size of a screen you’re dealing with. Canvas set to Overlay just is literally an overlay over your screen, but if set to be rendered by a Camera, it’s going to overlay that specific camera (You can have many cameras in your game). The Main Camera is the best option for objects that should never disappear from sight.

 

  1. Creating a Text object will automatically create a Canvas object, you can move it to wherever you want to see it. My recommendation is to change the basic color as it’s a very weak gray, and set the characteristics of the letters to OverFlow so they don’t stack weirdly.

 

  1. Now, a LevelManager will access your Player script and grab that information for other uses. Create an empty GameObject and call it LevelManager. Create a LevelManager Script and assign a Player Variable as we saw in point 5. In this tutorial, we also made a Public Text, but before that… you must place a “using UnityEngine.UI;” before the variable area. This will allow your script to grab UI information (which is where the Text is!). Drag and drop that text in the Inspector and now you can write whatever you want in it. How? With the use of this code:
    1. text = (“Whatever your text is. If you want a variable, do: “ + variablename);

 

Coding EXTREME basics II

 

So in this new edition of Coding Extreme Basics, I’ll just give you a small couple of new advices:

 

  1. USE DEBUG.LOG or PRINT. Debug.Log and Print will show you on the Console window anything you want to see on runtime. It’ll help you a lot. For example… sometimes, a part of your code works and the other seems to not do anything for no reason. No Reason. You should add a Debug.Log or a Print. Let’s say you want to add points. But it’s not doing anything, even when you think you did it all perfectly. You should add one of the options and see if the trigger is entering. Example:

 

  • void OnTriggerEnter2D (Collider2D hit){ Debug.Log(“This collided:” + hit.name) ; }
  • void OnTriggerEnter2D (Collider2D hit){ print(“This collided:” + hit.name) ; }

 

That way, you’ll see which game object entered the trigger. It can be really helpful.

 

  1. Don’t forget the semicolons. Don’t forget to READ your errors.

 

  1. Try adding only Circle and Box collider to your objects. They’re less resource intensive than Capsule and Polygon ones.

 

  1. Minimize the use of GetComponent or Find. Try to only use them on void Start().

 

  1. A very common error is “Object Reference Not Set”, which means you’re trying to access something that you haven’t told Unity “what” it is. Check your code for lack of references.

 

As always, ask questions in the comments or go to the Forum to ask anything else! I’ll be in touch.

 

 

 

 

 

 

 

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *