简体   繁体   English

“即使函数设置为void,“类,结构或接口方法也必须具有返回类型”

[英]“Class, struct, or interface method must have a return type” Even when function is set to void

I'm essentially trying to make a UI object fade in using Unity 2D's UI library. 我实质上是在尝试使用Unity 2D的UI库淡化UI对象。 The function fadeIn() starts a coroutine FadeCanvasGroup that slowly changes the alpha of the object using lerping in order to fade it in, the fadeOut(); 函数fadeIn()启动一个协程FadeCanvasGroup,它使用lerping缓慢更改对象的alpha,以便使其淡入淡出fadeOut();。 function simply reverses this. 函数只是将其反转。 However, I keep getting thrown "Class, struct, or interface method must have a return type" errors when calling fadeIn(), which confuses me, considering that fadeIn() and fadeOut() both are void functions. 但是,在调用fadeIn()时,我不断抛出“类,结构或接口方法必须具有返回类型”错误,这使我感到困惑,考虑到fadeIn()和fadeOut()都是无效函数。

I have changed functions to public and private, added and removed voids, and attempted to start the coroutine without the function, all of which met with failure. 我已将函数更改为公共和私有,添加和删除了空白,并尝试在没有该函数的情况下启动协程,所有这些都失败了。 As well, when attaching the function to a button using Unity's engine, it works fine, but when called through script, it fails. 同样,当使用Unity的引擎将功能附加到按钮时,它可以正常工作,但是当通过脚本调用时,它将失败。

//The element in question being defined(And later set using the engine)

public CanvasGroup uiElement;

//The coroutine in question

 private IEnumerator FadeCanvasGroup(CanvasGroup cg, float start, float end, 
 float lerpTime = 2f)
{

    float timeStartedLerping = Time.time;
    float timeSinceStarted = Time.time - timeStartedLerping;
    float percentageComplete = timeSinceStarted / lerpTime;
    while (true)
    {
        timeSinceStarted = Time.time - timeStartedLerping;
        percentageComplete = timeSinceStarted / lerpTime;

        float currentValue = Mathf.Lerp(start, end, percentageComplete);

        cg.alpha = currentValue;

        if (percentageComplete >= 1)
        {
            break;
        }

        yield return null;
    }
    print("done");
}

//The functions to fade in and out

    public void fadeIn()
    {
        StartCoroutine(FadeCanvasGroup(uiElement, uiElement.alpha, 1));
    }
    public void fadeOut()
    {
        StartCoroutine(FadeCanvasGroup(uiElement, uiElement.alpha, 0));
    }

fadeIn();

The fadeIn(); fadeIn(); being called at the end should just execute, but the earlier described error is taking place. 在末尾被调用应该只是执行,但是前面描述的错误正在发生。 Thanks for anyone's time who wishes to help with this. 感谢您抽出宝贵时间希望对此提供帮助的任何人。

Without a good Minimal, Reproducible Example , it's impossible to give you the exact fixed code. 如果没有一个好的最小的,可复制的示例 ,就不可能为您提供确切的固定代码。 However… 然而…

The way Unity3d uses C#, the error messages are sometimes not crystal clear. Unity3d使用C#的方式,有时错误消息不十分清晰。 But, you're getting that error because coroutine methods must return IEnumerable . 但是,您会收到此错误,因为协程方法必须返回IEnumerable

See the "fade" example in the Unity3d documentation for an illustration of how to do this correctly. 有关如何正确执行此操作的说明,请参见Unity3d文档中的“淡入淡出”示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 NUnit 2 升级到 NUnit 3 时出现错误“异步测试方法必须具有非空返回类型” - Error `Async test method must have non-void return type` when upgrading from NUnit 2 to NUnit 3 方法必须具有返回类型 - Method must have return type 方法必须具有返回类型,调试时不显示任何内容 - Method must have a return type, Not showing anything when Debugging 方法必须在InitializeComponent()方法上具有返回类型 - Method must have a return type on an InitializeComponent() method 接口方法返回类型是实现接口的类 - Interface method return type to be class that implements the interface 无法将void转换为int [],并且必须具有返回类型 - Cannot convert void to int[] and must have return type 为什么条件方法的返回类型必须为void? - Why does conditional methods must have a return type of void? 多播代表的返回类型必须为void。 为什么? - Multicast Delegates must have a return type of void. Why? c#接口方法必须具有从其自身派生的返回类型 - c# Interface method must have return type that derives from itself “方法必须具有返回类型”,并且“返回不能后跟对象表达式” - “Method must have a return type” and “return must not be followed by an object expression”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM