Overview: Accessing Other Game Objects 访问其他游戏物体
Most advanced game code does not only manipulate a single object. The Unity scripting interface has various ways to find and access other game objects and components there-in. In the following we assume there is a script named OtherScript.
Most advanced game code does not only manipulate a single object. The Unity scripting interface has various ways to find and access other game objects and components there-in. In the following we assume there is a script named OtherScript.js attached to game objects in the scene.
多数高级的游戏代码并不仅仅控制单独的游戏对象. Unity脚本有很多方法去查找和访问他们的游戏对象和组件.下面我们假设一个脚本OtherScript.js附于场景中的一个游戏对象上.
- C#
- JavaScript
1 2 3 4 |
|
1. Through inspector assignable references.
通过检视面板指定参数.
You can assign variables to any object type through the inspector:
你能通过检视面板为一些对象类型设置变量值:
- C#
- JavaScript
1 2 3 4 5 6 7 |
|
You can also expose references to other objects to the inspector. Below you can drag a game object that contains the OtherScript on the target slot in the inspector.
你也可以把参数显示在检视面板.随后你可以拖拽游戏对象OtherScript到检视面板中的target位置.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 10 |
|
2. Located through the object hierarchy.
确定对象的层次关系
You can find child and parent objects to an existing object through the Transform component of a game object:
你能通过游戏对象的 Transform 组件去找到它的子对象或父对象:
- C#
- JavaScript
1 2 3 4 5 6 |
|
Once you have found the transform in the hierarchy, you can use GetComponent to get to other scripts.
一旦你在层次视图找到transform,你便能用 GetComponent 获取其他脚本.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
You can loop over all children: 你能循环到所有的子对象:
- C#
- JavaScript
1 2 3 4 5 6 |
|
See the documentation for the Transform class for further information.
查看文档 Transform 类可以获得更多信息.
3. Located by name or Tag.
You can search for game objects with certain tags using GameObject.FindWithTag and GameObject.FindGameObjectsWithTag . Use GameObject.Find to find a game object by name.
GameObject.FindWithTag 和 GameObject.FindGameObjectsWithTag .使用 GameObject.Find 通过名字获得游戏对象.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 10 |
|
You can use GetComponent on the result to get to any script or component on the found game object
你可以用GetComponent获得指定游戏对象上的任意脚本或组件.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 |
|
Some special objects like the main camera have shorts cuts using Camera.main .
一些特殊对象,比如主摄像机,用快捷方式 Camera.main .
4. Passed as parameters. 传递参数
Some event messages contain detailed information on the event. For instance, trigger events pass the Collider component of the colliding object to the handler function.
一些事件包含详细的消息信息.例如,触发事件传递碰撞对象的 Collider 组件到处理函数.
OnTriggerStay gives us a reference to a collider. From the collider we can get to its attached rigidbody.
OnTriggerStay给我们一个碰撞体参数.通过这个碰撞体我们能得到它的刚体.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 |
|
Or we can get to any component attached to the same game object as the collider.
或者我们可以通过collider得到这个物体的任何组件.
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Note that by suffixing the other variable in the above example, you can access any component inside the colliding object.
注意, 在上面的例子中使用后缀的方式访问其他变量.同样,你能访问到碰撞对象包含的任意组件。
5. All scripts of one Type 某个类型的脚本
Find any object of one class or script name using Object.FindObjectsOfType or find the first object of one type using Object.FindObjectOfType .
找到某个类型的对象或脚本可以用 Object.FindObjectsOfType 或获得某个类型的第一个对象使用 Object.FindObjectOfType .
- C#
- JavaScript
1 2 3 4 5 6 7 8 9 |
|

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Win11XboxGameBar怎麼徹底卸載? XboxGameBar是系統中自帶的遊戲平台,它提供了用於遊戲錄製、截圖和社交功能的工具,不過很是佔用內存,也不好卸載,一些小伙伴想要將它卸載掉,但是不這道怎麼徹底卸載,下面就來為大家介紹一下。方法一、使用Windows終端機1、按【Win+X】組合鍵,或【右鍵】點選工作列上的【Windows開始功能表】,在開啟的的選單項目中,選擇【終端機管理員】。 2.用戶帳戶控制窗口,你要允許此應用程式對你的設備進行更改嗎?點選【是】。 3、執行以下指令:Get-AppxP

隨著遊戲發售日期的臨近,《黑神話:悟空》的炒作已在全球範圍內感受到,它在 8 月 20 日推出時並沒有令人失望,受到了整個遊戲社區的熱烈歡迎。上線後

以其前作《索尼克世代》(亞馬遜售價39 美元)的成功為基礎,世嘉將於2024 年10 月25 日發布《索尼克X 影子世代》。久,世嘉正在對原版遊戲進行擴充。

目前,《Sleeping Dogs: Definitive Edition》在 GOG 的大幅折扣價僅為 2.99 美元,較原價 19.99 美元大幅折扣 85%。要利用此優惠,只需訪問 GOG 上的遊戲頁面,添加

Square Enix 的《Foamstars》一推出就獲得了非常熱烈的反響,據報道在推出當天就擊敗了熱門遊戲《地獄潛水員 2》——這可能是因為它是作為 PS Plus 月度遊戲計劃的一部分推出的。然而,玩家數量很快就下降了

Hero of the Kindgom II 並不是目前在 Microsoft Store 中免費提供的唯一一款 Lonely Troops 遊戲。在 9 月 9 日之前,玩家還可以免費獲得 Townpolis,這是一款 2008 年推出的建築遊戲,通常售價 5 美元。 Townpolis 投入播放器

微軟公司今天針對Beta頻道發布Win11Build226×1.2271預覽版更新同時,邀請所有WindowsInsider頻道使用者體驗新版MicrosoftStore。 MicrosoftStore最新版本為22308.1401.x.x全新的GamePass頁面:微軟表示引進了全新的專用頁面,可以讓玩家探索並訂閱PCGamePass或GamePassUltimate。用戶可以了解GamePass諸多權益,包括獨佔遊戲、折扣、免費福利以及EAPlay等等。微軟希望用戶在不跳到

《王國英雄II》是一款帶有RPG元素的冒險遊戲,玩家扮演一個簡單的農民,和他的妹妹住在一個安靜的村莊。但田園詩般的生活很快就被海盜襲擊所擾亂,於是他們出發去拯救國王
