当前位置:网站首页>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,就立刻销毁物体
边栏推荐
- Go execute shell command
- 竞争与冒险 毛刺
- JIT deep analysis
- [JVM] detailed description of the process of creating objects
- Kotlin基础学习 17
- Failed to upgrade schema, error: “file does not exist
- spark调优
- 《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
- Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
- [HCIA continuous update] working principle of OSPF Protocol
猜你喜欢

Download and use of the super perfect screenshot tool snipaste
![[C Advanced] brother Peng takes you to play with strings and memory functions](/img/95/ab1bb0b3fa0b99e32233a5ca5d42a4.jpg)
[C Advanced] brother Peng takes you to play with strings and memory functions

Form custom verification rules

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

MySQL advanced (Advanced) SQL statement (II)

Generate random numbers that obey normal distribution

Detailed explanation of ThreadLocal

《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真

Discussion on related configuration of thread pool

The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
随机推荐
JIT deep analysis
MSI announced that its motherboard products will cancel all paper accessories
How to establish its own NFT market platform in 2022
spark调优
SAML2.0 notes (I)
Framing in data transmission
[HCIA continuous update] overview of dynamic routing protocol
What is the binding path of SAP ui5
[C Advanced] brother Peng takes you to play with strings and memory functions
Qualcomm platform WiFi -- P2P issue (2)
Getting started with MQ
数据传输中的成帧
C shallow copy and deep copy
In depth interpretation of pytest official documents (26) customized pytest assertion error information
MySQL advanced (Advanced) SQL statement (II)
Merge interval, linked list, array
Intersection of Venn graph
[database]jdbc
3048. Number of words
One of the future trends of SAP ui5: embrace typescript