Home > Back-end >  Cannot add script to a GameObject as a component
Cannot add script to a GameObject as a component

Time:11-25

VS screengrab

This is my full script

I get an error on line 15, beginning at the "PlayerControls" word.

error CS0246: The type or namespace name 'PlayerControls' could not be found (are you missing a using directive or an assembly reference?)

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

namespace puggy
{
    public class InputHandler : MonoBehaviour
    {
        public float horizontal;
        public float vertical;
        public float moveAmount;
        public float mouseX;
        public float mouseY;

        PlayerControls inputActions;

        Vector2 movementInput;
        Vector2 cameraInput;

        public void OnEnable()
        {
            if (inputActions == null)
            {
                inputActions = new PlayerControls();
                inputActions.PlayerMovement.Movement.performed  = inputActions => movementInput = inputActions.ReadValue<Vector2>();
                inputActions.PlayerMovement.Camera.performed  = i => cameraInput = i.ReadValue<Vector2>();
            }

            inputActions.Enable();

        }

        private void OnDisable()
        {
            inputActions.Disable();
        }

        public void TickInput(float delta)
        {
            MoveInput(delta);
        }

        private void MoveInput(float delta)
        {
            horizontal = movementInput.x;
            vertical = movementInput.y;
            moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal)   Mathf.Abs(vertical));
            mouseX = cameraInput.x;
            mouseY = cameraInput.y;
        }
    }
}

I'm guessing it wants a reference for "PlayerControls". I'm confused though because I followed a walkthrough, I ran into other issues but was able to resolve them pretty quickly. This one is tripping me up though.

enter image description here

I'm expecting to be able to attach this script to my player model and have it move based on player input.

I have re-typed my script and followed other forums to see if anyone else has had similar issues.

EDIT:

PlayerControls

CodePudding user response:

public class PlayerControls has to be in a file called PlayerControls.cs!

Looking at your screenshot you have InputHandler in a file called PlayerControls.cs

I guess you copied/renamed the class or file. Please make sure Filename and Classname match!

CodePudding user response:

I figured it out. When making the "PlayerControls Input Action" I forgot to also click "generate C# class".

This makes sure that all the stuff I set up in the GUI, is applied into the C# script.

  • Related