变量在当前上下文中不存在。

huangapple go评论69阅读模式
英文:

Variable Not Existing In Current Context

问题

在Unity中,我正在编写一个脚本,经过10秒后会跳转到一个新场景。我是通过启动一个协程来实现的。在具有与协程相同名称的IEnumerator中。但是仍然出现错误,显示“变量X在当前上下文中不存在”,我不知道为什么会发生这种情况。

以下是你的代码翻译部分:

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

public class Anim : MonoBehaviour
{
    float X = 1;
    
    void Start()
    {
        StartCoroutine(Timer());
    }

    void FixedUpdate()
    {
        if (X == 1f)
        {
            IEnumerator Timer()
            {
                yield return new WaitForSeconds(10f);
                SceneManager.LoadScene(1);
                float X = 0;
            }
        }
    }
}

我期望它在经过10秒后加载新场景,但是出现了错误。

英文:

So in unity I'm coding a script that takes you to a new scene after 10 seconds pass.
I'm doing it by starting a coroutine. In the IEnumerator that has the same name that the coroutine
has. But and still I have a error saying "The variable X does not exist in the current context" I have no idea why this is happening.

Here's my code:

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

public class Anim : MonoBehaviour
{
	float X = 1;
	
	void Start()
	{
		StartCoroutine(Timer());
	}

	void FixedUpdate()
	{
		if(X == 1f)
		{
			IEnumerator Timer()
			{
			yield return new WaitForSeconds(10f);
			SceneManager.LoadScene(1);
			float X =- 0;
			}
		}
		
	} 
	
}        

I was expecting it to load the new scene after 10 seconds have passed but it gave me an error.

答案1

得分: 0

你的问题在于,尽管 Timer() 函数还不存在,你尝试调用它。如果我没记错,Start 方法在你创建 IEnumerator 的 FixedUpdate 方法之前被调用。

你应该将创建 Timer() 的部分要么放在你调用 Timer() 的 Start 方法之前,要么完全放到那些方法之外,与 X 的位置相同。然后你的代码应该可以正常工作。

void Start()
{
    IEnumerator Timer()
    {
    [...]
    }
    StartCoroutine(Timer());
}

void FixedUpdate()
{
[...]
}

或者

float X = 1;
IEnumerator Timer()
{
    [...]
}

void Start()
{
    StartCoroutine(Timer());
}

void FixedUpdate()
{
[...]
}

(我不100%确定哪个选项最好,我可能会把它放到 Start 方法中。)

顺便说一句,尝试将你的代码以文本的形式添加到你的问题中(就像我上面所做的那样),而不是发布截图。

英文:

Your problem is that you try to call Timer() even though it doesn't exist yet. If I'm not mistaken the Start method is called before the FixedUpdate where you create the IEnumerator.

You should put the part that creates the Timer() either into the Start method above where you call Timer(), or you could put it completly outside of those methods at the same place as X. Then your code should work.

void Start()
{
    IEnumerator Timer()
    {
    [...]
    }
    StartCoroutine(Timer());
}

void FixedUpdate()
{
[...]
}

or

float X = 1;
IEnumerator Timer()
{
    [...]
}

void Start()
{
    StartCoroutine(Timer());
}

void FixedUpdate()
{
[...]
}

(I'm not 100% sure which is the best option, I'd probably put it into Start.)

Btw. Try to add your code as text into your question (as I've done above) instead of posting a screenshot.

huangapple
  • 本文由 发表于 2023年2月23日 21:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定