当前位置:网站首页>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
边栏推荐
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- Kotlin basic learning 14
- Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
- 表单自定义校验规则
- [database]jdbc
- C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
- Competition and adventure burr
- 终日乾乾,夕惕若厉
- UI (New ui:: MainWindow) troubleshooting
- C shallow copy and deep copy
猜你喜欢
QT environment generates dump to solve abnormal crash
命名块 verilog
Getting started with MQ
[HCIA continuous update] working principle of OSPF Protocol
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
汇率的查询接口
Generate random numbers that obey normal distribution
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
How to establish its own NFT market platform in 2022
随机推荐
Uniapp uses canvas to generate posters and save them locally
C # joint halcon out of halcon Environment and various Error Reporting and Resolution Experiences
Verilog 过程赋值 区别 详解
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Continuous assignment of Verilog procedure
Intersection vengraph
数据传输中的成帧
js生成随机数
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
How to establish its own NFT market platform in 2022
【DesignMode】建造者模式(Builder model)
Review materials of project management PMP high frequency examination sites (8-1)
Eight steps of agile development process
QT environment generates dump to solve abnormal crash
[yolo3d]: real time detection of end-to-end 3D point cloud input
PY3 link MySQL
C reflection practice
spark调优
aaaaaaaaaaaaa