using UnityEngine;
using VRStandardAssets.Utils;//引用此脚本
using UnityEngine.UI;//使用image类型添加脚本
public class VRManger : MonoBehaviour
{
private Image ring;
VRInteractiveItem vrItem;
private void Awake()
{
vrItem = GetComponent<VRInteractiveItem>() ??
gameObject.AddComponent<VRInteractiveItem>();//先判断是否有组件如果没有则加上,有就不加,并且加上后赋值给veitem
ring = GameObject.FindGameObjectsWithTag("ring")
.GetComponent<Image>();//查找ring,获得image组件
}
提示:Assets\Scripts\VRManger.cs(16,14): error CS1061: 'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)怎么办
GameObject[]是个数组啊,你要先遍历它,取得里面的每个GameObject,再访问它的属性
GameObject.FindGameObjectsWithTag("ring"),因为你是通过标签来获取物体,标签是可以重复的,那么获取到的一定是个集合
-=-=-=
其实如果你能够确定,你场景中的每个物体都有一个不同的标签,那么你完全可以这样写
ring = GameObject.FindGameObjectsWithTag("ring")[0].GetComponent();
但是不建议。最好还是先获取到GameObject.FindGameObjectsWithTag("ring")之后,判断一下数量,非0再继续操作,以免忘记放物体或者写错标签名字造成崩溃。