C# 如何在Drag.Update()中设置开始位置?

C# 如何在Drag.Update()中设置开始位置?,c#,unity3d,C#,Unity3d,我想做拖放游戏。游戏可以运行和玩,但有一些错误。根据下面的2个编码,有一个错误:- 完整编码:- Vector3 startposition; getTarget.transform.position = Vector3.Lerp(getTarget.transform.position, startposition, 1.0f); startposition = getTarget.GetComponent<Drag>().originalPosition; if ((d

我想做拖放游戏。游戏可以运行和玩,但有一些错误。根据下面的2个编码,有一个错误:-

完整编码:-

Vector3 startposition;
getTarget.transform.position = Vector3.Lerp(getTarget.transform.position, startposition, 1.0f);
startposition = getTarget.GetComponent<Drag>().originalPosition;
    if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
                {
                    startTime = Time.time;
                    journeyLength = Vector3.Distance(getTarget.transform.position, dropobject.transform.position);
                    correct = true;
                    correctdone = false;

                    if (dropobject.name == "ToasterTarget")
                    {
                        infoPanel1.SetActive(true);
                        infoPanel2.SetActive(false);
                        infoPanel3.SetActive(false);
                        infoPanel4.SetActive(false);
                        infoPanel5.SetActive(false);
                        infoPanel6.SetActive(false);
                        infoPanel7.SetActive(false);
                    }
}
    if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
                {
                    playAudioCorrect();
                    target.GetComponentInChildren<Renderer>().enabled = false;
                    getTarget.tag = "Untagged";

                    int tempscore = int.Parse(scoretext.GetComponent<Text>().text) + 50;
                    scoretext.GetComponent<Text>().text = tempscore.ToString();

                    int tempscore1 = int.Parse(finalScoreText.GetComponent<Text>().text) + 50;
                    finalScoreText.GetComponent<Text>().text = tempscore1.ToString();
                }
第80和105行控制台出错:-

NullReferenceException:对象引用未设置为对象的实例 Update()(位于Assets/GameApaNi/Scripts/Drag.cs:105)


您没有显示您实际遇到的错误,但考虑到您的代码,我只能假设您遇到了两个错误

NullReferenceException

第一种情况意味着未设置
getTarget
null
)-这不可能,因为否则您将在前面的行中得到错误-或者
getTarget.GetComponent()
返回
null
表示在
getTarget
上没有组件
拖动

如果它没有完全附加到
getTarget
对象,则可以使用它在层次结构中向上递归搜索,也可以使用它在层次结构中向下递归搜索


第二个几乎相同:未设置
dropobject
getTarget
,因此
null


对于这两种情况,您应该检查检查器引用,并逐行设置断点和代码


还有一些提示:

一般来说,为什么使用完全相同的
if
条件

if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
两次通话?你不能把这两个街区合并成一个街区吗

同时尽量避免重复调用
GetComponent
,只调用一次即可存储结果并重复使用引用,例如:

var scoreText = scoretext.GetComponent<Text>();

int tempscore = int.Parse(scoreText.text) + 50;
scoreText.text = tempscore.ToString();
然后将类型更改为

public Text scoretext;

您可以完全摆脱所有的
GetComponent
调用,这将使您的脚本更加高效(快速)。

您能解释一下什么没有按预期工作吗?会发生什么?您似乎知道错误的代码行,所以。。你认为他们到底有什么问题?哪一个错误?如果您在控制台上记录了任何内容,请将其添加到您的问题中。@derHugo已经编辑了我的问题。请帮助我。@Adda不,我很抱歉,但这绝对违反社区规则。我提供了一个如何在运行时访问代码的链接。。使用它。仅一个引用未设置。。找出原因!也有一个阅读
var scoreText = scoretext.GetComponent<Text>();

int tempscore = int.Parse(scoreText.text) + 50;
scoreText.text = tempscore.ToString();
public GameObject scoretext;
public Text scoretext;