当前位置:网站首页>Unity脚本的基础语法(7)-成员变量和实例化
Unity脚本的基础语法(7)-成员变量和实例化
2022-07-02 03:25:00 【ht_game】
成员变量
一般情况下,定义在方法体外的变量是成员变量,如果这个变量为public类型的,就可以在属性面板中看到。若在属性面板对它的值进行修改,它的值就会随着项目一起自动保持
公共变量
public int a=1;
可以在属性面板中看到这个变量,名字为a,它默认显示的值为“1”,读者可以随时在属性面板中修改它的值
如果声明的是一个组件类型的变量(类似GameObject、Transform、Rigidbody等),则需要在属性面板中将游戏对象拖拽到变量处并确定它的值。
public class variable : MonoBehaviour {
public Transform ren;
void Update () {
if (Vector3.Distance(ren.position,transform.position) > 10)//如果ren和transform的距离大于10
{
Debug.Log(ren.position);
}
}
}
私有变量
可以通过private关键字创建私有变量,这些变量在属性面板中不会显示,可以避免被修改。
private Transform ren;
void Update () {
if (Vector3.Distance(ren.position,transform.position) > 10)//如果ren和transform的距离大于10
{
Debug.Log(ren.position);
}
}
静态成员变量
在C#脚本中可以通过static关键字来创建全局变量,这样就可以在不同脚本间调用该变量。
public static int test;
如果从另一个脚本中调用变量Test,可以通过“脚本名.变量名”的方法来调用。
public class Test:MonoBehaviour{
void Start()
{
Test.test=1;//为Test脚本中的test变量赋值
}
}
实例化游戏对象
在Unity中如果想创建游戏对象,可以通过创建游戏对象菜单在场景中创建游戏对象。这些游戏对象在场景加载时被创建出来;也可以在脚本中动态地创建游戏对象。在游戏运行过程中,根据需要在脚本中实例化游戏对象的方法更加灵活。
如果想在Unity中创建很多相同的物体(如枪射出的子弹)时,可以通过实例化(Instantiate)快速实现。实例化出来的游戏对象包含该对象所有的属性,这样就能保证原封不动地创建所需的对象。实例化在Unity中很多用途,合理使用它非常有必要。
实例化的方法
Destroy(gameObject,n)方法是在n秒后销毁物体。如果想立刻销毁物体,可以使用DestroyImmediate(gameObject,boolean),如果参数的布尔值为true,就立刻销毁物体
边栏推荐
- Delphi xe10.4 installing alphacontrols15.12
- Verilog 状态机
- Generate random numbers that obey normal distribution
- Detailed explanation of ThreadLocal
- Possible causes of runtime error
- Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
- Grpc快速实践
- Go执行shell命令
- Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
- < job search> process and signal
猜你喜欢
[JS reverse series] analysis of a customs publicity platform
表单自定义校验规则
知物由学 | 自监督学习助力内容风控效果提升
Retrofit's callback hell is really vulnerable in kotlin synergy mode
寻找重复数[抽象二分/快慢指针/二进制枚举]
Framing in data transmission
On redis (II) -- cluster version
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Verilog parallel block implementation
随机推荐
Discrimination between sap Hana, s/4hana and SAP BTP
Verilog state machine
Verilog reg register, vector, integer, real, time register
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Verilog 过程赋值 区别 详解
Merge interval, linked list, array
Go执行shell命令
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
汇率的查询接口
Verilog avoid latch
Global and Chinese markets for infant care equipment, 2022-2028: Research Report on technology, participants, trends, market size and share
表单自定义校验规则
Spark Tuning
PMP personal sprint preparation experience
Intersection vengraph
Verilog parallel block implementation
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
verilog 并行块实现
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Go execute shell command