c# - How to get reference to GameObject without using Find() in C#/Unity?
问题描述
I am currently taking an intro to games programming class and in the lecture notes it mentions that instead of using something like:
GameObject obj = GameObject.Find("name");
inside functions, you can "add object as Public member of type GameObject and connect it in the inspector" without actually explaining how to do it.
I tried to lookup how to do this but was unable to find how to properly do so, would appreciate any help.
解决方案
Here is how you could do it. Lets say you want obj2
in obj1
First go in the script of obj1
and declare a public
variable public GameObject obj2;
Then go in to the inspector of the obj1
, on script component you will find a field Obj2
. Now just drag and drop the obj2
from hierarchy in that field or click a circular button next to field and select the object from the list.
推荐阅读
- java - 如何使用检票口禁用单选组单选按钮?
- javascript - 欧拉计划 #29:为什么我的答案是 37?
- ansible - 如何解决 ansible win_hotfix 模块的问题
- azure - 对象中缺少必需的属性:qnaRuntimeEndpoint
- java - 由于“org.hibernate.internal.SessionFactoryImpl”检测到内存泄漏
- java - 在日历 SWT 中为日期设置一些条件
- c# - IEnumerable
比较 - javascript - 混合内容:“https://example.com/sw.js”处的页面是通过 HTTPS 加载的,但请求了不安全的资源“http://example.com/about/”
- r - 我可以从函数参数创建值名称,并为其分配函数输出吗?
- django - 如何在 django 模型中打印字段的字段类型