c# - Find a common script that is attached to multiple gameObjects?
问题描述
I have a script that has been attached to multiple gameObjects and this script I am referencing in another script. However when referred, only one gameObject is accessed instead of accessing all. How do I access all the gameObjects that use this script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckForGrab : MonoBehaviour {
private OVRGrabber grabber; //the script I try to access
// Use this for initialization
void Start () {
grabber = (OVRGrabber) FindObjectOfType(typeof(OVRGrabber)); // the gameObject the script is attached to
}
}
解决方案
如果您想要多个结果,则必须查询多个实例。明显地。
尝试FindObject s OfType。注意's'。
此外,在这种情况下,使用泛型而不是强制转换。
grabbers = FindObjectsOfType<OVRGrabber>();
grabber = grabbers[index];
推荐阅读
- r - 如何使用ggplot2从具有非等距轴的矩阵绘制等高线图?
- laravel - 通用请求字段的全局定义规则
- java - 在java中并排显示来自磁盘和URL的图片
- javascript - 是的,使用匹配问题使用正则表达式进行验证
- python-3.x - Python 3:函数参数中的省略号?
- excel - VBA将一列的单元格值以数值(MMDD)开头的数据行复制到另一个工作表
- python - 如何在numpy中组合数组?
- angular - 后台同步不会自动触发
- android - AnimationDrawable android studio中的问题
- neo4j - 如何获取网络内社区中具有特定标签的节点数量?