【问题标题】:Error CS1525: Unexpected symbol `)', expecting `(', `[', or `{'错误 CS1525:意外符号 `)',需要 `('、`[' 或 `{'
【发布时间】:2018-10-08 10:31:24
【问题描述】:

我编写了一些代码,当发生碰撞时禁用脚本并启动协程,但我收到此错误 Assets/Scripts/SceneDelay.cs(16,40): 错误 CS1525: Unexpected symbol ), 期待 (, [, or { 我已经搜索了这个错误,但在我的情况下没有一个答案对我有帮助,我的代码对我来说似乎很完美,我不明白它有什么问题。这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SceneDelay : MonoBehaviour {

public static int score = 0;
public Text scoreText;

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "Obstacle")
    {
        GetComponent(new ScoreScript).enabled = false;
        StartCoroutine(DelayLoad());
    }
}

IEnumerator DelayLoad()
{
    yield return new WaitForSeconds(1);

    SceneManager.LoadScene("Menu");
    scoreText.text = 0.ToString();
    score = 0;

    yield break;
}
}

【问题讨论】:

  • new ScoreScript => new ScoreScript()
  • yield break; 是多余的

标签: c# compiler-errors ienumerable enumerator


【解决方案1】:

这条线应该是,

GetComponent(new ScoreScript()).enabled = false;

您错过了用于创建新实例的 ()。

【讨论】:

  • 好的,完成了,但现在它给了我这两个错误:Assets/Scripts/SceneDelay.cs(16,13): error CS1502: The best overloaded method match for UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments**. and **Assets/Scripts/SceneDelay.cs(16,26): error CS1503: Argument # 1' 无法转换ScoreScript' expression to type System.Type'
  • 显示引发异常的实际代码。 ScoreScript 显然是您正在启动的其他类,它不是 GetComponent() 所期望的已定义 System.Type。这就像你有一个需要一个整数参数的方法,然后你尝试将它传递给其他类型,例如字符串等。
  • 代码在我发布的问题中。以及如何将我的 ScoreScript 更改为已定义的 System.Type
  • @Hanaan328 - 我怀疑你想要类似GetComponent<ScoreScript>().enabled = false;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
相关资源
最近更新 更多