当前位置:网站首页>Basic syntax of unity script (4) - access to other game objects
Basic syntax of unity script (4) - access to other game objects
2022-06-30 13:01:00 【ht_ game】
Most scripts don't just control the GameObjects attached to them ,Unity There are many ways to access in scripts Other Game objects and game components . Can pass Property panel assign parameters To get the game object , It can also be done through Find Method to get the game object .
Specify parameters through the property panel
Declare... In the code public Type of GameObject reference , The game object parameters will be displayed in the property panel , Then you can drag the game object to be obtained to the relevant parameter position of the property panel .
public GameObject otherObject
void Update()
{
Test test=otherObject.GetComponent<Test>();// obtain Test Script components
}
Determine the hierarchical relationship of objects
The game object has a parent-child relationship in the game composition object list , In the code, you can get Transform Component to find the child or parent object
public class Find : MonoBehaviour {
void Update()
{
transform.Find("Son").Translate(0, 0, 1 * Time.deltaTime, Space.Self);// Find child objects “Son”, Let it move
transform.parent.Translate(0,0,1*Time.deltaTime,Space.Self);// Find the parent object , Let it move
}
}
Get child objects in , You can go through GetComponent Method to get other components of the child object , It can also be called directly GetComponentInChildren And GetComponentInParent Method to get components on parent and child objects
for example
public class Find2 : MonoBehaviour {
void Update () {
transform.GetComponentInChildren<Text>().text = "123";
}
}
You can also use scripts to cycle through all the child objects , Then do some operation on the child object
public class Find2 : MonoBehaviour {
void Update()
{
foreach (Transform child in transform)// Loop through all child objects
{
child.GetComponent<Text>().text = " white ";
}
}
}
Get game objects by name or tag
Unity You can use FindWithTag Methods and Find Method to get the game object ,FindWithTag Method to get the game object with the specified label ,Find Method to get the game object with the specified name .
GameObject name=GameObject.Find("Somename");
GameObject tag=GameObject.FindWithTag("Sometag");
Get the game object by passing parameters
Some event callback method parameters contain special GameObject or component information , Such as triggering the collision event Collider Components . stay OnTriggerStay Method has an collider parameter , Through this parameter, we can get the rigid body of collision .
void OnTriggerStay(Collider other)
{
if(other.GetComponent<Rigidbody>())// If the GameObject has a rigid body component
{
other.GetComponent<Rigidbody>().AddForcr(0,0,2);// Apply a force to the rigid body
}
}
Get the game object by component name
Unity Scripts are available through FindObjectsOfType Methods and FindObjectOfType Method to find the GameObject to which a particular type of component is attached .FindObjectsOfType Method can get all game objects that mount components of the specified type
FindObjectOfType Method can get the first game object that mounts the component of the specified type
Test test=FindObjectOfType<Test>();// Get the first found Test Components
Test[] tests=FindObjectsOfType<Test>();// Get all Test Components
边栏推荐
- Illustration creating a stored procedure using Navicat for MySQL
- ffmpeg 杂项
- Flinksql customizes udatf to implement topn
- JS converts an array to a two-dimensional array based on the same value
- In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
- 顺应媒体融合趋势,中科闻歌携手美摄打造数智媒宣
- 独立站即web3.0,国家“十四五“规划要求企业建数字化网站!
- App wechat payment unicloud version of uniapp payment (with source code)
- Dark horse notes -- wrapper class, regular expression, arrays class
- Kubeedge's core philosophy
猜你喜欢

Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18

QT read / write excel--qxlsx worksheet display / hide status setting 4

Qt读写Excel--QXlsx工作表显示/隐藏状态设置4

黑马笔记---包装类,正则表达式,Arrays类

ABAP toolbox v1.0 (with implementation ideas)

RK356x U-Boot研究所(命令篇)3.2 help命令的用法
![[surprised] the download speed of Xunlei is not as fast as that of the virtual machine](/img/04/b0d23266b70c9ad6990a5203ef8ddf.png)
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine

顺应媒体融合趋势,中科闻歌携手美摄打造数智媒宣

RK356x U-Boot研究所(命令篇)3.3 env相关命令的用法

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)
随机推荐
深度长文探讨Join运算的简化和提速
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年6月29日21:59:34
Event handling in QT
Can the polardb MySQL fees for RDS MySQL data migration be transferred?
postman 自動生成 curl 代碼片段
Illustration creating a stored procedure using Navicat for MySQL
Shell基础入门
Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
kaniko官方文档 - Build Images In Kubernetes
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
JS method of changing two-dimensional array to one-dimensional array
【C】深入理解指针、回调函数(介绍模拟qsort)
Product manager professional knowledge 50 (7) - how to establish a complete set of user growth system?
Postman génère automatiquement des fragments de code Curl
Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
rpm2rpm 打包步骤
Unity脚本的基础语法(3)-访问游戏对象组件
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer
【OpenGL】OpenGL Examples
RK356x U-Boot研究所(命令篇)3.3 env相关命令的用法