Home > Net >  C# error's question as type or namespace issue
C# error's question as type or namespace issue

Time:11-29

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;
        }


    }

}


I have 7 error about CS0246. I used all namespace whatever I can use but it does not work. I checked the briket and semicolon all of them but i am not sure why i get error. I used **

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

** Those namespace but it does not work please help me............ **Please note the parenthesis which are causing the issue I have labelled Error Parethesis however I am not entirely sure these are causing the overall issue.

CodePudding user response:

Try to put a space between the using System.Diagnostics; and the monobehaviour and also make sure it has the same name as the script file you created.

CodePudding user response:

Reading the error messages:

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?)

RigidBody requires a reference to UnityEngine.PhysicsModule

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?)

Animator requires a reference to UnityEngine.AnimationModule

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?)

SerializeField requires a using reference to UnityEngine.CoreModule

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?)

Should this be PlayerMovement rather than PlayMovement?

  • Related