当前位置:网站首页>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,就立刻销毁物体
边栏推荐
- js生成随机数
- Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
- Kotlin基础学习 14
- JS generate random numbers
- Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
- [数据库]JDBC
- 《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
- Cache processing scheme in high concurrency scenario
- Verilog wire type
- Common means of modeling: aggregation
猜你喜欢
OSPF LSA message parsing (under update)
Delphi xe10.4 installing alphacontrols15.12
Design details of SAP e-commerce cloud footernavigationcomponent
Force deduction daily question 540 A single element in an ordered array
Screenshot literacy tool download and use
[JVM] detailed description of the process of creating objects
[golang] leetcode intermediate bracket generation & Full Permutation
C shallow copy and deep copy
Framing in data transmission
Just a few simple steps - start playing wechat applet
随机推荐
[HCIA continuous update] working principle of OSPF Protocol
Verilog timing control
Work hard all day long and be alert at sunset
Go执行shell命令
Design details of SAP e-commerce cloud footernavigationcomponent
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
Mmsegmentation series training and reasoning their own data set (3)
venn圖取交集
Verilog state machine
Grpc quick practice
跟着CTF-wiki学pwn——ret2shellcode
ThreadLocal详解
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
4. Find the median of two positive arrays
How to establish its own NFT market platform in 2022
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
初出茅庐市值1亿美金的监控产品Sentry体验与架构
Apple added the first iPad with lightning interface to the list of retro products
Kotlin basic learning 13
Review materials of project management PMP high frequency examination sites (8-1)