我正在构建一个方法,在GameObjects数组中返回一个"Type".每个GameObject都附加了一个组件,所以我在尝试这个没有结果.
private Type GetComponentOfType(string name)
{
var component; // Error here
for (int i = 0; i < sceneObjectsLength; i++)
{
component = sceneObjects[i].GetComponent(Type.GetType(name));
}
return component;
}
Run Code Online (Sandbox Code Playgroud)
举例说明了这个想法:
private Start()
{
Circle circle:
circle = GetComponentOfType("Circle");
Debug.Log(circle.Radius());
Square square;
square = GetComponentOfType("Square");
Debug.Log(square.Sides());
}
Run Code Online (Sandbox Code Playgroud)