当前位置:网站首页>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中
边栏推荐
- 第十三章 信号(三)- 示例演示
- 力扣之螺旋矩阵,一起旋转起来(都能看懂)
- Redis installation on Linux system
- “\“id\“ contains an invalid value“
- Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
- 两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
- Redis-缓存问题
- Two batches of pure milk are unqualified? Michael responded that he was conducting a large-scale screening and sampling inspection of products
- Substrate 源码追新导读: Pallet Alliance 并入主线,
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
猜你喜欢

【OpenGL】OpenGL Examples

Three ways for flinksql to customize udaf

Idea has a new artifact, a set of code to adapt to multiple terminals!

【一天学awk】基础中的基础

Determining the subject area of data warehouse construction

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms

New function of SuperMap iserver11i -- release and use of legend

Redis - problèmes de cache

Q-learning notes

市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套
随机推荐
Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
【一天学awk】运算符
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer
Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions
【 surprise】 la vitesse de téléchargement de Thunderbolt n'est pas aussi rapide que celle de la machine virtuelle
Substrate 源码追新导读: 质押额度大幅度削减, RocksDB可以完全被Disable
Joplin implements style changes
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
【一天学awk】正则匹配
Why should offline stores do new retail?
Videos are stored in a folder every 100 frames, and pictures are transferred to videos after processing
grep匹配查找
Hisilicon 3559 universal platform construction: introduction to YUV format
Clipboardjs - development learning summary 1
Swagger2 automatically generates API documents
如何利用AI技术优化独立站客服系统?听听专家怎么说!
FlinkSQL自定义UDATF实现TopN
Four ways for flinksql to customize udtf
Redis-缓存问题
Questionnaire star questionnaire packet capturing analysis