Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetComponent从4升级到5不起作用_C#_Unity3d - Fatal编程技术网

C# GetComponent从4升级到5不起作用

C# GetComponent从4升级到5不起作用,c#,unity3d,C#,Unity3d,在我的Unity 4.3中,所有的功能都很好,但是升级到5之后,我的GetComponent出现了问题。为了测试一个新的不推荐的GetComponent,我使用了官方教程 using UnityEngine; using System.Collections; public class test : MonoBehaviour { public GameObject otherGameObject; private AnotherScript anotherScript; void Awake

在我的Unity 4.3中,所有的功能都很好,但是升级到5之后,我的GetComponent出现了问题。为了测试一个新的不推荐的GetComponent,我使用了官方教程

using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public GameObject otherGameObject;
private AnotherScript anotherScript;
void Awake ()
{
    anotherScript = GetComponent<AnotherScript>();
}

void Update ()
{
    Debug.Log("The player's score is " + anotherScript.playerScore);
}
}
这只是为了测试

我使用了unity教程的相同示例

之后,我在编辑器中关联了两个对象,但运行报告是:

NullReferenceException:对象引用未设置为对象的实例 Update()(位于Assets/test.cs:22)


在unity 4.3中工作得很好。

您应该尝试在Start方法中获取引用。确保
test
AnotherScript
脚本都附加到编辑器中的同一个游戏对象,那么下面的代码应该可以工作

using UnityEngine;
using System.Collections;

public class test: MonoBehaviour 
{
    public int playerScore;

    void Start()
    {
        anotherScript = gameObject.GetComponent<AnotherScript>();
    }

    void Update ()
    {
       Debug.Log("The player's score is " + anotherScript.playerScore);
    }
}
使用UnityEngine;
使用系统集合;
公共课堂测试:单一行为
{
公共int playerScore;
void Start()
{
anotherScript=gameObject.GetComponent();
}
无效更新()
{
Log(“玩家的分数是”+anotherScript.playerScore);
}
}
如果两个脚本未连接到同一游戏对象,则使用:

 anotherScript = GameObject.Find("Name of GameObject AnotherScript is Attched To").GetComponent<AnotherScript>();
anotherScript=GameObject.Find(“另一个脚本所连接的GameObject的名称”).GetComponent();

您确定另一个脚本也附加到您测试脚本附加到的同一游戏对象上吗

GetComponent方法只查找附加到游戏对象的组件


简单的方法当然是将另一个脚本成员公开(因此它在inspector中公开),然后将脚本拖放到其中以获得引用。

如果脚本未连接到同一个游戏对象,则需要使用
FindObjectOfType()

我认为应该是这样的:

public GameObject otherGameObject;


private AnotherScript anotherScript;


void Awake ()
{
    anotherScript = otherGameObject.GetComponent<AnotherScript>();

}

void Update ()
{
    Debug.Log("The player's score is " + anotherScript.playerScore);
}

}
公共游戏对象其他游戏对象;
另一个私人脚本另一个脚本;
无效唤醒()
{
anotherScript=otherGameObject.GetComponent();
}
无效更新()
{
Log(“玩家的分数是”+anotherScript.playerScore);
}
}

您应该在编辑器中附加其他GameObject。

尝试向
AnotherScript
类添加构造函数,如
public AnotherScript(){playerScore=9001;}
这也意味着当声明变量时,您应该删除分配了value@AlfieGoodacre. 不。永远不要在源自
monobhavior
@JoeBlow的统一脚本中使用构造函数。我理解这一点,但我不希望删除它,因为它不会对我产生负面影响,因为它不能被否决。不仅如此,它还会给程序员带来很多麻烦,因为除了基本的错误,他的评论也不再有意义了。没有人提到你有一个大问题。基本上,您不应该拥有一个既为“公共”又为其指定“默认”值的变量(9001)。这确实会导致大规模的混乱。请阅读并投票。嗨@AlfieGoodacre我同意Mugi很酷。我可以向你保证,最好的办法是删除你的前两条评论。(A) 程序员,谁是异常警觉的一般以及经验丰富的网站,将知道删除他们的评论。(事实上,任何使用该网站超过2周的人都会知道这一点。)(B)即使在程序员没有这样做的特殊情况下,如果没有这两条误导性评论,互联网也会更好。在团结的环境中,误导性的评论有一个很大的问题。你今天过得很愉快当我说在开始时做它时,我的意思是
GetComponent()在启动功能中,而不是唤醒。-1不是我的。为您修复了它。@但它应该处于唤醒状态。当前的答案毫无意义,您将这两个脚本合并为一个。@FINDarkside。不,不必在醒着的时候做。假设两个脚本都连接到同一个游戏对象,这应该可以工作。@FINDarkside。我看到了你的评论,不想回复,因为它一直在继续,在这个问题上它不是一个重要的论点。您所说的取决于项目和脚本的结构。例如,如果ObjectA依赖ObjectB工作,ObjectB应该在
Awake
函数中初始化,ObjectA应该在
Start
函数中初始化,否则会出现空错误。@FINDarkside当您的项目越来越大,有30多个脚本相互对话时,您可能会注意到这一点。执行我上面所说的操作通常会修复它,或者您必须在编辑器中的脚本执行顺序设置中手动执行它。所以这取决于。。。。当你说“它应该处于唤醒状态”时,这是一种误导,会让人们犯错误或无法解决他们的空异常问题,因为他们在线阅读变量应该在
Awake
函数中初始化,而不是在
Start
函数中初始化。
public GameObject otherGameObject;


private AnotherScript anotherScript;


void Awake ()
{
    anotherScript = otherGameObject.GetComponent<AnotherScript>();

}

void Update ()
{
    Debug.Log("The player's score is " + anotherScript.playerScore);
}

}