简体   繁体   English

如何修复“当前上下文中不存在名称‘脚本’”?

[英]how do I fix "The name 'script' does not exist in the current context"?

I have this code below, but I get an error and can't fix it.我在下面有此代码,但出现错误且无法修复。 The error:错误:

The name script does not exist in the current context.当前上下文中不存在名称script

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

public class FollowCube: MonoBehaviour
{

    public GameObject cube;
    public Vector3 cubePov;
    public Vector3 cubePovStart;
    public Vector3 camPov;

    void Start()
    {
        cube = GameObject.Find("Cube");
        CollisionDetection script = cube.GetComponent<CollisionDetection>();
    }

    void Update()
    {
        cubePov = (GameObject.Find("Cube")).transform.position;
        camPov = transform.position;
        if (script.CollisionDown == true)
        {
            
        }
    }
}

How do I fix it?我如何解决它?

script exists just in Start() method; script只存在于Start()方法中; so that' why it is not visible in the Update() method.所以这就是它在Update()方法中不可见的原因。 Declare it globally, and then reuse it (in case if those are static class methods).全局声明它,然后重用它(以防它们是静态类方法)。

So what that means is, the variable 'script' hasn't been declared in the Update() method.所以这意味着,在Update()方法中还没有声明变量 'script'。 You're declaring 'script' in the Start() method.您在Start()方法中声明了“脚本”。 If you need it to be available to both declare it up next to the "public Vector3" and then both locations can access it.如果您需要它同时在“公共 Vector3”旁边声明它,然后两个位置都可以访问它。 But do note, if you declare it, it has to be set to something before it can be used still.但是请注意,如果您声明它,则必须先将其设置为某些内容才能继续使用。 In the below, I moved its scope so both can access it.在下面,我移动了它的范围,以便两者都可以访问它。

Again to reiterate, Start() must be called before Update() otherwise a null exception will occur because you initialize it (set it to something) in Start() .再次重申,必须在Update()之前调用Start()否则将发生空异常,因为您在Start()对其进行了初始化(将其设置为某些内容Start()

I'm not well versed in Unity but if there is any chance that GameObject.Find might return a null object if what you're asking for is not found then you would want to put a check in to handle that or at least be aware of it.我不太精通 Unity,但如果GameObject.Find可能返回一个空对象,如果你要求的东西没有找到,那么你会想要检查来处理这个问题,或者至少要注意其中。

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

public class FollowCube : MonoBehaviour
{
    public GameObject cube;
    public Vector3 cubePov;
    public Vector3 cubePovStart;
    public Vector3 camPov;
    public CollisionDetection script;

    void Start()
    {
        cube = GameObject.Find("Cube");
        script = cube.GetComponent<CollisionDetection>();
    }

    void Update()
    {
        cubePov = (GameObject.Find("Cube")).transform.position;
        camPov = transform.position;

        if (script.CollisionDown == true)
        {
            
        }
    }
}

Variables declared inside methods can not be used in other methods.在方法内部声明的变量不能在其他方法中使用。 You are declaring script in Start() , but trying to use it in Update() .您正在Start()中声明script ,但尝试在Update()使用它。 Just make it a global variable (aka declare it on the top of your code) and you should be good to go!只需将其设为全局变量(也就是在代码顶部声明它),您就可以开始使用了!

暂无
暂无

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

相关问题 如何解决“当前上下文中不存在名称&#39;页面&#39;” - How do I fix “The name 'page' does not exist in the current context” 当前上下文中不存在如何解决名称“名称”和“值”的问题 - How to fix the name 'Name' & 'Value' does not exist in the current context 当前上下文中不存在名称“Fix” - The name 'Fix' does not exist in the current context 如何解决&#39;当前上下文中不存在&#39;WebSecurity&#39;这个名字&#39;? - How to fix 'The name 'WebSecurity' does not exist in the current context'? 如何解决&#39;名称&#39;k&#39;在当前上下文中不存在&#39;的错误? - How to fix 'The name 'k' does not exist in the current context' error? 该名称在当前上下文Unity脚本中不存在 - The name does not exist in the current context Unity script 名称...在当前上下文中不存在...我们如何切换上下文? - The name … does not exist in the current context… how do we switch contexts? 如何解决Razor Pages错误“ CS0103当前上下文中不存在名称&#39;Json&#39;”? - How do I solve the Razor Pages error “CS0103 The name 'Json' does not exist in the current context”? 如何在Jquery中正确调用C#变量。 “变量名称”在当前上下文中不存在 - How do I properly call a C# variable in Jquery. The 'variable name' does not exist in the current context 当前上下文中不存在名称“queen” - 如何将Box附加到数据中? - The name 'queen' does not exist in the current context- How do I attach the Box to the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM