Unity Learn home
View Tutorial Content
Steps

Creating Properties

Tutorial
Intermediate
+10 XP
5 Mins
(3040)
Summary
Skills
How to create properties to access the member variables (fields) in a class.
Select your Unity version
Last updated: January 11, 2022
2019.3
2019.2
2019.1
2018.4
2018.3
2018.2
2018.1
2017.4
2017.3
2017.2
2017.1
5.x
4.x
Language
English
Also included in

1.Properties

Video Player is loading.
Current Time 0:00
Duration 0:00
Loaded: 0%
Stream Type LIVE
Remaining Time 0:00
 
1x

Player
using UnityEngine; using System.Collections; public class Player { //Member variables can be referred to as //fields. private int experience; //Experience is a basic property public int Experience { get { //Some other code return experience; } set { //Some other code experience = value; } } //Level is a property that converts experience //points into the leve of a player automatically public int Level { get { return experience / 1000; } set { experience = value * 1000; } } //This is an example of an auto-implemented //property public int Health{ get; set;} }
Game
using UnityEngine; using System.Collections; public class Game : MonoBehaviour { void Start () { Player myPlayer = new Player(); //Properties can be used just like variables myPlayer.Experience = 5; int x = myPlayer.Experience; } }

Creating Properties
Creating Properties
General Tutorial Discussion
0
0
1. Properties
161
36