如何从Unity中的其他组件访问自定义属性

时间:2013-07-23 13:14:24

标签: unity3d unityscript

如何从其他Unity组件访问自定义属性?

在我的头像组件中,我已经声明了这样的变量:

public var isInEndzone : boolean;

从另一个组件调用该属性:

//actor variable is the GameObject that has the Avatar component.
public var avatar : GameObject;

....

var avatarComponent : Avatar = avatar.GetComponent(Avatar);
if (avatarComponent.IsInEndzone){
//do something...
}

错误:

GetComponent requires that the requested component 'Avatar' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject:GetComponent(Type)

BCE0019: 'IsInEndzone' is not a member of 'UnityEngine.Avatar'. 

1 个答案:

答案 0 :(得分:1)

我知道了!您不能使用保留名称Avatar,显然已经是一个类,将名称更改为其他名称,它将起作用。