【问题标题】:Unable to fix monobehaviour scripts problem in Unity无法修复 Unity 中的单一行为脚本问题
【发布时间】:2019-07-01 03:09:57
【问题描述】:

我刚刚开始使用 Unity 并使用它的标准资源库。我使用了它的滚球对象,但它的 Ball 和 BallUserControl 脚本都给了我以下错误:

文件中没有 Monobehaviour 脚本,或者名称与文件名不匹配。

我没有更改任何脚本,并且我的环境中没有其他对象或脚本。类名与文件名匹配。我确信我错过了一些明显的东西(如此新)。有哪些可能的错误? 我在 Windows 10 上运行最新版本的统一。

这是 BallUserControl 脚本,根据要求:

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Vehicles.Ball
{
    public class BallUserControl : MonoBehaviour
    {
        private Ball ball; // Reference to the ball controller.

        private Vector3 move;
        // the world-relative desired move direction, calculated from the camForward and user input.

        private Transform cam; // A reference to the main camera in the scenes transform
        private Vector3 camForward; // The current forward direction of the camera
        private bool jump; // whether the jump button is currently pressed


        private void Awake()
        {
            // Set up the reference.
            ball = GetComponent<Ball>();


            // get the transform of the main camera
            if (Camera.main != null)
            {
                cam = Camera.main.transform;
            }
            else
            {
                Debug.LogWarning(
                    "Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
                // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
            }
        }


        private void Update()
        {
            // Get the axis and jump input.

            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
            jump = CrossPlatformInputManager.GetButton("Jump");

            // calculate move direction
            if (cam != null)
            {
                // calculate camera relative direction to move:
                camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
                move = (v*camForward + h*cam.right).normalized;
            }
            else
            {
                // we use world-relative directions in the case of no main camera
                move = (v*Vector3.forward + h*Vector3.right).normalized;
            }
        }


        private void FixedUpdate()
        {
            // Call the Move function of the ball controller
            ball.Move(move, jump);
            jump = false;
        }
    }
}

【问题讨论】:

  • 上传 BallUserControl 脚本
  • 更新了我的问题以包含脚本。如果您还需要什么,请告诉我
  • 您使用的是哪个 UnityVersion?资产与它兼容吗?他们说CrossPlatformInputManager is deprecated
  • 我没有收到该错误。我从统一本身中导入了资产。我用的是2019.3.0a7版本。
  • 不一定与问题直接相关,但一般来说:2019.3.0a7alpha 版本(因此是小 a)。与任何其他 alpha 版本一样,它仅用于测试新功能,对于生产而言不稳定。它不太可能充满小错误和错误......这就是拥有 alpha 和 beta 版本的全部目的。查看该页面上的Known Issues 部分。如果您想真正使用 Unity 来创建一些东西,请坚持使用最新的稳定版本 - 目前是 2019.1.8

标签: c# unity3d


【解决方案1】:

如果 monobeviour 文件丢失或未在 Visual Studio 中显示并产生错误,请转到 Edit/Preferences/External Tools/ 选择 V​​isual Studio 版本并 重新生成项目文件。它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多