当前位置:网站首页>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
边栏推荐
- 【OpenGL】OpenGL Examples
- 60 个神级 VS Code 插件!!
- Substrate 源码追新导读: 质押额度大幅度削减, RocksDB可以完全被Disable
- Postman génère automatiquement des fragments de code Curl
- Unity脚本的基础语法(5)-向量
- MySQL implements the division of two query results
- Tronapi wave field interface PHP version - interface document attached - package based on thinkphp5 - source code without encryption - can be opened in two - detailed guidance of the author - 11:49:56
- Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
- 第十三章 信号(三)- 示例演示
- 机器学习笔记 - 自相关和偏自相关简介
猜你喜欢
Dark horse notes - common date API
Shell基础入门
FlinkSQL自定义UDAF使用的三种方式
Js根据相同值将数组转换为二维数组
IDEA 2021.3 执行 golang 报错:RNING: undefined behavior version of Delve is too old for Go version 1.18
rxjs Observable 两大类操作符简介
黑马笔记---集合(Collection的常用方法与遍历方式)
Determining the subject area of data warehouse construction
Rk356x u-boot Institute (command section) 3.3 env related command usage
杭州电子商务研究院:官网(网站)是私域的唯一形态
随机推荐
深度长文探讨Join运算的简化和提速
postman 自動生成 curl 代碼片段
Questionnaire star questionnaire packet capturing analysis
Rk356x u-boot Institute (command section) 3.3 env related command usage
Wechat applet reports an error: typeerror: cannot read property 'SetData' of undefined
[one day learning awk] use of built-in variables
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
机器学习笔记 - 自相关和偏自相关简介
[yitianxue awk] regular matching
Unity脚本的基础语法(4)-访问其他游戏对象
第十三章 信号(三)- 示例演示
Substrate 源码追新导读: 质押额度大幅度削减, RocksDB可以完全被Disable
【C】深入理解指针、回调函数(介绍模拟qsort)
[learn awk in one day] operator
今日睡眠质量记录80分
Definition of variables and assignment of variables in MySQL
The spiral matrix of the force buckle rotates together (you can understand it)
MATLAB小技巧(22)矩阵分析--逐步回归
Problems and analysis in JMeter performance testing. How many problems have you encountered?
Unity的脚本的基础语法(2)-Unity中记录时间