TheDeveloperBlog.com

Home | Contact Us

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

Unity Arrays

Unity Arrays 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

Arrays

An array is used to store a sequential collection of values of the same type. In short, an array is used to store lists of values in a single variable. Suppose if you want to store a number of player names, rather than storing them individually as string p1, string p2, string p3, etc. we can store them in an array.

Arrays are a way of storing a collection of data of the same type together.

Declaring an array

To declare an array in C#, you must first say what type of data will be stored in the array. After the type, specify an open square bracket and then immediately a closed square bracket, []. This will make the variable an actual array. We also need to specify the size of the array. It simply means how many places are there in our variable to be accessed.

Syntax:

accessModifier datatype[] arrayname = new datatype[arraySize];

Example:

public string[] name = new string[4];

To allocate empty values to all places in the array, simply write the "new" keyword followed by the type, an open square bracket, a number describing the size of the array, and then a closed square bracket.

Example

Let's see one simple example using an array:

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

public class ArrayExample : MonoBehaviour
{
    public int[] playerNumber= new int[5];

    void Start()
    {
        for (int i = 1; i < playerNumber.Length; i++)
        {
            playerNumber[i] = i;
            Debug.Log("Player Number: " +i.ToString());

        }
    }
}

Output:

Arrays




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