脚本没有继承一个可以管理脚本的本地类。

问题描述 投票:0回答:1

大家好,我是unity的初级用户。我是unity的初级用户,在学习unity的时候,我的C#脚本文件不能插入到prefabs中,并且返回错误信息:该脚本没有继承一个可以管理脚本的本地类。

这是我的C#脚本代码。

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

public class unPlayerMovement
{
    private Transform tr;

    public float moveSpeed = 30.0f;
    public float rotSpeed = 150.0f;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;



tr = GetComponent<Transform>();


    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Mouse X");
        float v = Input.GetAxis("Vertical");

        Move();
    }
    void Move(float h, float v)
    {
        tr.Rotate(0, v * moveSpeed * Time.deltaTime, 0);
        tr.Translate(0, 0, h * rotSpeed * Time.deltaTime);
    }
}



And this is my Unity screen.

请在这里输入图片描述

unity3d
1个回答
0
投票

你需要继承MonoBehaviour。

public class unPlayerMovement:MonoBehaviour

0
投票

你的Move()也有错误[修正],这可能是unity没有加载脚本的原因。如果你需要任何帮助,请告诉我:)

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

public class unPlayerMovement : MonoBehaviour {

private Transform tr;

public float moveSpeed = 30.0f;
public float rotSpeed = 150.0f;
// Start is called before the first frame update
void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    tr = GetComponent<Transform>();
}

// Update is called once per frame
void Update()
{
    float h = Input.GetAxis("Mouse X");
    float v = Input.GetAxis("Vertical");

    Move(h,v);
}
void Move(float h, float v)
{
    tr.Rotate(0, v * moveSpeed * Time.deltaTime, 0);
    tr.Translate(0, 0, h * rotSpeed * Time.deltaTime);
}
}
© www.soinside.com 2019 - 2024. All rights reserved.