Variable in C#
There are different type of variable in C# these are :
Int: values like : 1,2,3,4,5,-1,-2,-3,-4..
Float: values like 1.23, 3.44 , 5.48, 9.32 , -12.34....
in C# for float variable we will use "f" after the number exp:
1.23f, 3.44f, 5.48f etc.
String: exp , "unity3D" , " augmented reality" , "Virtual reality"...
Private Variable: These variable are accessible in side the class only.So user cant change its value from the inspector panel.
Public variables are those variable which will be visible in game scene view and we can change their value from the inspector panel itself.
See below code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cubescript : MonoBehaviour
{
public int intvalue = 0;
public float floatvalue = 2.34f;
public string total = "Value sum : ";
float sum = 0,privatesum = 0;
private int privateintvalue = 10;
private float privatefloatvalue = 10.12f;
void Start()
{
print("Stated !");
}
private void Update()
{
sum = intvalue + floatvalue;
Debug.Log("Public var : -- "+total+" : "+sum);
privatesum = privateintvalue + privatefloatvalue;
Debug.Log("Private var : -- " + privatesum);
}
}
Implementation in Unity:
in above image of cube inspector panel we can see in Cubescript , Public variables Intvalue, Floatvalue, and Total are shown here(Here we can change these value from inspector).
In Console Window we can see the result for above script.
Public variable : sum of intvalue and floatvalue, see the inspector panel.
Private variable : 2.34 is the sum of privateintvalue and privatefloatvalue, see the above c# script.
now we will change the value for public variable shown in inspector panel through inspector panel itself and see the result:
In above image we can see , we have changed the value of intvalue to 10 from 0 , Floatvalue to 15.5 from 2.34 and Total (String value) to total from Value sum.
After changing these values we got this result, see below:
After changing the public variable from the inspector we can see the result in console window for above script.
Public variable : sum of intvalue and floatvalue, see the inspector panel.
Private variable : -- 20.12 is the sum of privateintvalue and privatefloatvalue, see the above c# script.
Thank you :-)
0 Comments:
Post a Comment
Thank you for your comment !