DEV Community

LoganVoisin
LoganVoisin

Posted on • Edited on

Usefull Unity Functions

Unity is one of many amazing ways to make games. While there is a learning curve most things can be learned rather quickly. However the scripting side of unity can be quite hard even if you know basic programming concepts.There are a lot of unity specific functions that you need to know to be able to do what you want in unity
The first main concept that we need to know is vector math. So a^2 + b^2 = c^2, that is if we think in terms of 2D. With this we can tell unity where to move any game object as long as it’s in 2D. The function for this is Vector2(), if you want to move something in 3d then you'd use Vector3().
Next up is Awake() and Start(). Awake() is called before Start(), and Awake will be called even if a script is disabled. So it’s best used to set up references between game objects. Start() is called before the first update of the game object and only if the script is enabled.
Now we have FixedUpdate and Update. FixedUpdate runs at a constant time so it’s best used for game objects that have physics. Update is called as soon as it can so it's time can vary by a lot.
Up next we have GetButton() and GetKey(). GetButton() is used for controller support and it’s how we detect a button press. GetKey()is how we check for key presses on the keyboard. There's also GetKeyDown() to see when a button was pressed down and GetKeyUp() to see when it was released. We also have OnMouseDown to see when we have clicked a button on the mouse. We can also get the cursor position as well with Input.mousePosition;.
The next helpful funtion to know is transform.LookAt(target);. What this will do is point a game object's transform face another game objects. This is how we could program enemies to look at a player. We could also create a thrid person camera with this function as well. We could do that by making the camera face the player game object.
Then we also have two more functions that go almost hand in hand with one another, Destroy() and Instantiate(). Destroy() removes a spesifyed game object after a certain distance it travils or after a certain amount of time. Instantiate() create a new instance of a prefab game object. It also has many different options you can control, such as it's speed, rotation, position, and many other factors.
What if we want to just disable a component? Well then we could use enabled for that. enabled works like this

void Update ()
    {
        if(Input.GetKeyUp(KeyCode.Space))
        {
            myLight.enabled = !myLight.enabled;
        }
    }
Enter fullscreen mode Exit fullscreen mode

what that will do is when you press space the ling will flip it's enabled status. Allowing you to turn the light off and on.
But, what if you wanted to enable and/or disable game objects? Well then I have the perfict function for you, setActive(). Below is an example of setActive()

public class ActiveObjects : MonoBehaviour
{
    void Start ()
    {
        gameObject.SetActive(false);
    }
}
Enter fullscreen mode Exit fullscreen mode

using this we can enable and/or disable game objects using scripts insted of the unity editor.
Those are just a few of the basic functions that are useful to know when starting unity. There are plenty of others but we don’t have enough time to get into all of them. I hope this can be a good starting point for your unity journey.

Top comments (0)