当前位置:网站首页>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
边栏推荐
- 初出茅庐市值1亿美金的监控产品Sentry体验与架构
- Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Kotlin basic learning 15
- C shallow copy and deep copy
- SAML2.0 notes (I)
- Gradle 笔记
- Competition and adventure burr
- Redis set command line operation (intersection, union and difference, random reading, etc.)
- Intersection of Venn graph
- verilog REG 寄存器、向量、整数、实数、时间寄存器
猜你喜欢

Continuous assignment of Verilog procedure

Unity脚本的基础语法(6)-特定文件夹

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

Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency

JIT deep analysis

《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
![[JS reverse series] analysis of a customs publicity platform](/img/15/fdff7047e789d4e7c3c273a2f714f3.jpg)
[JS reverse series] analysis of a customs publicity platform

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

Common means of modeling: aggregation

跟着CTF-wiki学pwn——ret2shellcode
随机推荐
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
Just a few simple steps - start playing wechat applet
aaaaaaaaaaaaa
Retrofit's callback hell is really vulnerable in kotlin synergy mode
Use blocking or non blocking for streamline
Framing in data transmission
Verilog 状态机
uniapp 使用canvas 生成海报并保存到本地
Eight steps of agile development process
spark调优
【DesignMode】建造者模式(Builder model)
Verilog reg register, vector, integer, real, time register
MySQL connection query and subquery
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
What is hybrid web containers for SAP ui5
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
[JS reverse series] analysis of a customs publicity platform
Which of PMP and software has the highest gold content?
Failed to upgrade schema, error: “file does not exist
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