TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Unity Variables and Functions

Unity Variables and Functions with Introduction, Installing, GameObject, First Unity Project, Unity 2D, Sprite Unity, Loops, If Statement, Data Types, Swith Statements, Unity UI, Unity Asset Store etc.

<< Back to UNITY

Variables and Functions

GameObjects have components that make them behave in a certain way. When we refer to components, we are referring to the available functions of a GameObject, for example, the human body has many functions, such as moving, eating, talking, and observing. Now let's say that we want the human body to move faster. That means movement function is linked to that action. So to make our human body to move faster, we would need to create a script that had access to the movement component, and then we would use that to make the body move faster.

Just like in real life, different GameObjects can also have different components; for example, the camera component can only be accessed from a camera. Many components already exist that were created by Unity's programmers, but we can also create our own components.

This means that all the properties that we see in the Inspector tab are just variables of some type. They simply store data that will be used by some function.

Public Variables

If you put the public at the beginning of a variable statement means that the variable will be visible and accessible. It will be visible as a property in the Inspector panel so that you can change the value stored in the variable.

Example:

public int num = 10;

Private Variables

Not all the variables needed to be public. If there is no need for a variable to be changed in the Inspector panel or be accessed from other scripts.

Example:

private int myNum = 2;

Example

Let's create a script with the name myTest.cs that has one int variable:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myTest : MonoBehaviour
{
    int num = 24;
    
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Now create a GameObject (from the top menu GameObject-> Create Empty) and add the myTest.cs script file to the GameObject (in the Inspector panel of newly created GameObject). Now it will look in the GameObject's Inspector:

Variables and Functions in Unity

Here, we can see that it has myTest.cs script file, but it does not have our num variable at all.

Let's modify our script by adding a new variable, but this time with the public prefix. In a programming language, the public means that the value can be seen by other classes too. In Unity, the public also means that the variable will be shown in the Inspector.

The modified myTest.cs script is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myTest : MonoBehaviour
{
    int num = 24;
    public string name;

    void Start()
    {
        Debug.Log("My name is " + name);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Just like other components often have properties that are editable in the Inspector panel, we can allow values in our script to be edited from the Inspector too.

Variables and Functions in Unity

Enter the value for variable Name and then press play. You will see that the message includes the text you entered.

Variables and Functions in Unity

Hence, in Unity, you must declare a variable as public to see it in the Inspector.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf