C# 错误CS0165:使用未分配的局部变量';sot';

C# 错误CS0165:使用未分配的局部变量';sot';,c#,unity3d,C#,Unity3d,我正在努力处理这两行代码! 首先,我得到一个错误: “错误CS1929:'AvatarTracking'不包含'GetOradComponent'的定义,并且最佳扩展方法重载'UnityEngineeExtensions.GetOradComponent(GameObject)'需要'GameObject'类型的接收器” 然后我将代码更改为: StaticOffsetTransform sot = this.GetOrAddComponent<StaticOffsetTransform&

我正在努力处理这两行代码! 首先,我得到一个错误: “错误CS1929:'AvatarTracking'不包含'GetOradComponent'的定义,并且最佳扩展方法重载'UnityEngineeExtensions.GetOradComponent(GameObject)'需要'GameObject'类型的接收器”

然后我将代码更改为:

 StaticOffsetTransform sot = this.GetOrAddComponent<StaticOffsetTransform>();
StaticOffsetTransform sot=this.getOradComponent();

staticoffsettransformsot;
sot=sot.gameObject.getOradComponent();
现在我得到了这个错误“错误CS0165:未赋值局部变量'sot'的使用” 请分享你解决这个问题的想法

p、 s:我甚至试过这两种:

StaticOffsetTransform sot = new GameObject.GetOrAddComponent<StaticOffsetTransform>();
StaticOffsetTransform sot = (Instantiate sot as GameObject).GetOrAddComponent<StaticOffsetTransform>();
StaticOffsetTransform sot=new GameObject.getOradComponent();
StaticOffsetTransform sot=(将sot实例化为GameObject);

但还是不行

问题在第二行

StaticOffsetTransform sot;
sot = sot.gameObject.GetOrAddComponent<StaticOffsetTransform>();
      ^^^
staticoffsettransformsot;
sot=sot.gameObject.getOradComponent();
^^^
在初始化sot之前,您正在使用它(并且您也将得到一个空引用异常)

要解决这个问题,您必须检查“如何获取StaticOffsetTransform实例”的逻辑

为了解决CS0165的特定问题,在向变量添加新引用之前,或从方法调用、属性、变量等其他方式获取引用之前,不得使用该变量

兼容代码的一个示例:

StaticOffsetTransform sot = components.GetOrAddComponent<StaticOffsetTransform>();
StaticOffsetTransform sot=components.getOradComponent();
参考:

在分配之前,您正在使用
sot
。我刚刚编辑了问题。还是同样的原因吗@DanielA.Whitenew GameObject()不能重新声明变量。理想情况下,变量只能用于一件事。
StaticOffsetTransform sot = components.GetOrAddComponent<StaticOffsetTransform>();