当前位置:网站首页>Unity脚本的基础语法(3)-访问游戏对象组件
Unity脚本的基础语法(3)-访问游戏对象组件
2022-06-30 12:18:00 【ht_game】
组件属于游戏对象,由于所有的脚本都是组件,因此一般的脚本都可以附加到游戏对象上。
常用的组件可以通过简单的成员变量取得
组件 | 变量 |
---|---|
Transform | transform |
Renderer | renderer |
Light | collider |
Rigidbody | rigidbody |
Camera | Camera(只有摄像机对象有效) |
Animation | animation |
这里的组件体现在属性面板上,而变量是在脚本中体现的。
在Unity中,附加到游戏对象上的组件可以通过GetComponent方法获得,具体可以如此使用
沿x轴正方向移动1个单位
void Update () {
transform.Translate(1*Time.deltaTime, 0, 0);
GetComponent<Transform>().Translate(1 * Time.deltaTime, 0, 0);
}
transform是变量,Transform是类或脚本。大小写不同可使开发人员能够从类和脚本名中区分变量。
同样也可以通过GetComponent方法获取其他脚本。例如
已有一个HelloWorld类,并且类中有一个**sayHello()**方法
void Update () {
HelloWorld helloWorld=GetComponent<HelloWorld>();//获取HelloWorld脚本脚本
helloWorld.sayHello();//执行sayHello方法
}
如何要在脚本中只执行一次可以在Start和Awake中
边栏推荐
- 图解使用Navicat for MySQL创建存储过程
- 两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
- Sublist3r error reporting solution
- ffmpeg 杂项
- Scratch drawing square electronic society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- Tencent two sides: @bean and @component are used on the same class. What happens?
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- Pinda general permission system (day 7~day 8)
- Terms related to JMeter performance test and performance test passing standards
- [surprised] the download speed of Xunlei is not as fast as that of the virtual machine
猜你喜欢
MySQL built-in functions
Analysis of the whole process of common tilt data processing in SuperMap idesktop
【OpenGL】OpenGL Examples
60 divine vs Code plug-ins!!
Questionnaire star questionnaire packet capturing analysis
Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
MATLAB小技巧(22)矩阵分析--逐步回归
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
7 lightweight and easy-to-use tools to relieve pressure and improve efficiency for developers, and help enterprises' agile cloud launch | wonderful review of techo day
Introduction to new features of ES6
随机推荐
Three ways for flinksql to customize udaf
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine
SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries
Pinda general permission system (day 7~day 8)
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions
MySQL判断执行条件为NULL时,返回0,出错问题解决 Incorrect parameter count in the call to native function ‘ISNULL‘,
FlinkSQL自定义UDTF使用的四种方式
Google refutes rumors and gives up tensorflow. It's still alive!
Browser plays RTSP video based on nodejs
Flink SQL console, group not recognized_ Concat function?
电机控制Clarke(α/β)等幅值变换推导
Grep match lookup
黑马笔记---List系列集合与泛型
Docker安装Mysql8和sqlyong连接报错2058的解决方法[随笔记录]
Substrate 源码追新导读: Pallet Alliance 并入主线,
Shell编程概述
机器学习笔记 - 自相关和偏自相关简介
Dataworks synchronizes maxcomputer to sqlserver. Chinese characters become garbled. How can I solve it
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?