unity3d C#错误的问题为类型或命名空间问题

gj3fmq9x  于 2022-11-30  发布在  C#
关注(0)|答案(2)|浏览(381)
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;  
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float movementSpeed =100f;

    private float verticalDirection;

    private Rigidbody rb;

    private Animator animator;
    // Start is called before the first frame update
      public bool IsMoving()
    {
        return rb.velocity.magnitude > 0.1f;
    }
    
    void Awake()
    {
        rb=GetComponent<Rigidbody>();
        animator = GetComponentInChildren<Animator>();
    }
    private void Update()
    {
        verticalDirection=Input.GetAxis("Vertical");
        verticalDirection=Mathf.Clamp(verticalDirection,0,1);

        animator.SetFloat("Speed",verticalDirection);
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        rb.velocity=Vector3.forward*verticalDirection*movementSpeed*Time.fixedDeltaTime;
    }
}

Error1:
Assets\Scripts\PlayerMovement.cs(4,31): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?)

Error2:
Assets\Scripts\PlayerMovement.cs(10,13): error CS0246: The type or namespace name 'Rigidbody' could not be found (are you missing a using directive or an assembly reference?)
Error3: 
Assets\Scripts\PlayerMovement.cs(12,13): error CS0246: The type or namespace name 'Animator' could not be found (are you missing a using directive or an assembly reference?)
Error4:
Assets\Scripts\PlayerMovement.cs(12,13): error CS0246: The type or namespace name 'Animator' could not be found (are you missing a using directive or an assembly reference?)
Error5:
Assets\Scripts\PlayerMovement.cs(6,6): error CS0246: The type or namespace name 'SerializeFieldAttribute' could not be found (are you missing a using directive or an assembly reference?)
Error6:
Assets\Scripts\PlayerMovement.cs(6,6): error CS0246: The type or namespace name 'SerializeField' could not be found (are you missing a using directive or an assembly reference?)
Error7:
Assets\Scripts\Robot.cs(20,13): error CS0246: The type or namespace name 'PlayMovement' could not be found (are you missing a using directive or an assembly reference?)

It is my other code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;  
//using System.Runtime.Serialization;
//using UnityEngine.UI;
//using System.web.Helpers;

enum RobotStates {Counting,Inspecting}
public class Robot : MonoBehaviour
{
    [SerializeField] private float startInspectionTime = 2f;
    [SerializeField] private AudioSource jingleSource;
    private float currentInspectionTime;

    private RobotStates currentState = RobotStates.Counting;

    private Animator animator;

    private PlayMovement player;

    // Start is called before the first frame update
    void Start()
    {
        player=FindObjectType<PlayerMovement>();
        animator=GetComponentInChildren<Animator>();

        currentInspectionTime = startInspectionTime;
    }

    // Update is called once per frame
    void Update()
    {
        StateMachine();
    }

    private void StateMachine()
    {
        switch(currentState)
        {
            case RobotStates.Counting:
                Count();
                break;
            case RobotStates.Inspecting:
                Inspect();
                break;
            default:
                break;
        }

    }

    private void Count()
    {
        if (!jingleSource.isPlaying){
            animator.SetTrigger("Look");
            currentState=RobotStates.Inspecting;
        }
    }

    private void Inspect()
    {
        if(currentInspectionTime>0)
        {
            currentInspectionTime -=Time.deltaTime;
        
            if (player.IsMoving())
            {
                Destroy(player.gameObject);
            }
        }
        else
        {
            currentInspectionTime=startInspectionTime;
            animator.SetTrigger("Look");

            jingleSource.Play();
            currentState=RobotStates.Counting;
        }

    }

}

我有7个关于CS0246的错误。我使用了所有我可以使用的命名空间,但它不工作。我检查了所有的胸和分号,但我不知道为什么我得到错误。我使用了**

using UnityEngine;
using UnityEngine.Video; 
using UnityEngine.UI;
using UnityEditor;
using System.Diagnostics;

**这些命名空间不起作用,请帮助我............**请注意引起问题的括号,我已将其标记为Error Parethesis,但我不完全确定这些括号是否引起了整体问题。

bq8i3lrv

bq8i3lrv1#

尝试在使用System.Diagnostics之间放置一个空格;和monobhavior,并确保它与您创建的脚本文件具有相同的名称。

vcudknz3

vcudknz32#

阅读错误消息:
错误2:资源\脚本\玩家移动。cs(10,13):错误CS0246:找不到类型或命名空间名称“Rigidbody”(是否缺少using指令或程序集引用?)
RigidBody需要对UnityEngine.PhysicsModule的引用
错误3:资源\脚本\玩家移动.cs(12,13):错误CS0246:未能找到类型或命名空间名称“Animator”(是否缺少using指令或程序集引用?)错误4:资源\脚本\玩家移动.cs(12,13):错误CS0246:找不到型别或命名空间名称'Animator'(您是否遗漏using指示词或组件指涉?)
Animator需要对UnityEngine.AnimationModule的引用
错误5:资源\脚本\玩家移动。cs(6,6):错误CS0246:未能找到类型或命名空间名称“SerializeFieldAttribute”(是否缺少using指令或程序集引用?)错误6:资源\脚本\玩家动作.cs(6,6):错误CS0246:未能找到类型或命名空间名称“SerializeField”(是否缺少using指令或程序集引用?)
SerializeField需要对UnityEngine.CoreModuleusing引用
错误7:资源\脚本\机器人.cs(20,13):错误CS0246:找不到类型或命名空间名称“PlayMovement”(是否缺少using指令或程序集引用?)
这是否应该是PlayerMovement而不是PlayMovement

相关问题