当前位置:网站首页>Basic syntax of unity script (7) - member variables and instantiation
Basic syntax of unity script (7) - member variables and instantiation
2022-07-02 03:28:00 【ht_ game】
Member variables
In general , Variables defined outside the method are member variables , If this variable is public Type of , You can see in the property panel . If you modify its value in the property panel , Its value will be automatically maintained with the project
Public variables
public int a=1;
You can see this variable in the property panel , The name is a, The default value displayed is “1”, Readers can modify its value in the property panel at any time
If you declare a variable of component type ( similar GameObject、Transform、Rigidbody etc. ), You need to drag the game object to the variable in the property panel and determine its value .
public class variable : MonoBehaviour {
public Transform ren;
void Update () {
if (Vector3.Distance(ren.position,transform.position) > 10)// If ren and transform The distance is greater than 10
{
Debug.Log(ren.position);
}
}
}
Private variables
Can pass private Keyword to create private variables , These variables are not displayed in the property panel , Can avoid being modified .
private Transform ren;
void Update () {
if (Vector3.Distance(ren.position,transform.position) > 10)// If ren and transform The distance is greater than 10
{
Debug.Log(ren.position);
}
}
Static member variable
stay C# In the script, you can use static Keyword to create global variables , In this way, the variable can be called between different scripts .
public static int test;
If you call a variable from another script Test, Can pass “ Script name . Variable name ” Method to call .
public class Test:MonoBehaviour{
void Start()
{
Test.test=1;// by Test Script test Variable assignment
}
}
Instantiate game object
stay Unity If you want to create game objects , You can create game objects in the scene through the create game object menu . These game objects are created when the scene is loaded ; You can also dynamically create game objects in scripts . In the course of the game , The method of instantiating game objects in scripts as needed is more flexible .
If you want to Unity Create many of the same objects in ( Like a bullet from a gun ) when , It can be instantiated (Instantiate) Fast implementation . The instantiated game object contains all the attributes of the object , This ensures that the required objects are created intact . Instantiate in Unity Many uses in , It is very necessary to use it reasonably .
Instantiation method
Destroy(gameObject,n) The method is in n Destroy the object in seconds . If you want to destroy the object immediately , have access to DestroyImmediate(gameObject,boolean), If the Boolean value of the parameter is true, Destroy the object immediately
边栏推荐
- Xlwings drawing
- Pointer array & array pointer
- GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
- verilog 并行块实现
- In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
- Verilog 状态机
- halcon图像矫正
- C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- In depth interpretation of pytest official documents (26) customized pytest assertion error information
猜你喜欢

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud

Sentry experience and architecture, a fledgling monitoring product with a market value of $100million

数据传输中的成帧

Verilog 避免 Latch

Verilog timing control

Verilog parallel block implementation

SAML2.0 笔记(一)

verilog 并行块实现

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
随机推荐
Discrimination between sap Hana, s/4hana and SAP BTP
焱融看 | 混合云时代下,如何制定多云策略
uniapp 使用canvas 生成海报并保存到本地
表单自定义校验规则
PY3 link MySQL
跟着CTF-wiki学pwn——ret2shellcode
Exchange rate query interface
Verilog 状态机
Just a few simple steps - start playing wechat applet
spark调优
Kotlin基础学习 16
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
MySQL之账号管理
[database]jdbc
Eight steps of agile development process
What is hybrid web containers for SAP ui5
[golang] leetcode intermediate bracket generation & Full Permutation
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
JIT deep analysis
PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip