当前位置:网站首页>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,就立刻销毁物体
边栏推荐
- 终日乾乾,夕惕若厉
- Design details of SAP e-commerce cloud footernavigationcomponent
- Verilog 过程连续赋值
- Which of PMP and software has the highest gold content?
- This article describes the step-by-step process of starting the NFT platform project
- uniapp 使用canvas 生成海报并保存到本地
- Halcon image rectification
- Discussion on related configuration of thread pool
- Grpc quick practice
- Verilog 状态机
猜你喜欢

Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性

Halcon image rectification

ThreadLocal详解

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

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

Verilog parallel block implementation

SAML2.0 notes (I)

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

Force deduction daily question 540 A single element in an ordered array

PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
随机推荐
js生成随机数
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Kotlin 基础学习13
跟着CTF-wiki学pwn——ret2shellcode
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Competition and adventure burr
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
aaaaaaaaaaaaa
Uniapp uses canvas to generate posters and save them locally
Verilog 时序控制
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
What is the binding path of SAP ui5
3048. Number of words
Getting started with MQ
Kotlin basic learning 13
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Intersection of Venn graph
汇率的查询接口
寻找重复数[抽象二分/快慢指针/二进制枚举]