我们可以通过用代码 Find 对象的名字从而获得指定的对象。Find 会遍历 Hierarchy 里面的组件,找到第一个符合时就会终止遍历。而通过实验可以发现,遍历是从底部开始至上面,而不是从顶部遍历到下面,这个很关键。

GameObjectmyCube=GameObject.Find("Cube");
Rendererrender=myCube.GetComponent();
render.material.color=Color.red;
通过上面的代码,我们可以获得第一个Cube对象,并且将他的颜色变成红色。

网友评论