当前位置:网站首页>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
边栏推荐
- Verilog 过程连续赋值
- Intersection vengraph
- Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
- Kotlin basic learning 17
- Go execute shell command
- Gradle 笔记
- [HCIA continuous update] overview of dynamic routing protocol
- "Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
- Grpc快速实践
- 焱融看 | 混合云时代下,如何制定多云策略
猜你喜欢

Exchange rate query interface

Review materials of project management PMP high frequency examination sites (8-1)

How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?

PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip

Verilog 过程赋值 区别 详解

Pycharm2021 delete the package warehouse list you added

Knowing things by learning | self supervised learning helps improve the effect of content risk control

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Design details of SAP e-commerce cloud footernavigationcomponent

Verilog 时序控制
随机推荐
竞争与冒险 毛刺
终日乾乾,夕惕若厉
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
Intersection of Venn graph
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
Gradle 笔记
Uniapp uses canvas to generate posters and save them locally
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
【DesignMode】建造者模式(Builder model)
Design details of SAP e-commerce cloud footernavigationcomponent
Spark Tuning
Pycharm2021 delete the package warehouse list you added
数据传输中的成帧
Intersection vengraph
[HCIA continuous update] working principle of OSPF Protocol
js生成随机数
C#联合halcon脱离halcon环境以及各种报错解决经历
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Gradle notes