简体   繁体   English

C#和Unity出现使我感到困惑的错误

[英]C# and Unity getting errors that confuse me

I am brand new at this, and I am having a few conundrums about this code that I put in. I followed a tutorial and I am still having some issues. 我对此是全新的,并且对所输入的代码有一些难题。我遵循了教程,但仍然遇到一些问题。 All I want to do is move the object that I have this script attached to called Player, and the script is PlayerMovement. 我想要做的就是移动附着了此脚本的对象Player,该脚本是PlayerMovement。

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

public class PlayerMovement : MonoBehaviour {

    public object Player(PlayerMovement) { // Identifier expected, and 'PlayerMovement.Player(PlayerMovement)' not all codes return a value
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Transform.Translate(Vector3.forward * Time.deltaTime);  //ERROR: An object reference is required for the non-static field, method or property 'Transform.Translate(vector3)

            Debug.Log("W is pressed, and I should move forwards.");
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("S is pressed, and I should move backwards.");
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("A is pressed, and I should move left.");
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("D is pressed, and I should move right.");
        }
    }
}

On line 22 I am getting 2 errors Identefier expected, and 'PlayerMovement.Player(PlayerMovement)' not all codes return a value. 在第22行,我收到2个Identefier预期的错误,并且'PlayerMovement.Player(PlayerMovement)'并非所有代码都返回一个值。

On line 29 I am getting An object reference is required for the non-static field, method or property 'Transform.Translate(vector3)' 在第29行上,我得到非静态字段,方法或属性'Transform.Translate(vector3)'的对象引用是必需的

As I am brand new, I have been looking up answers for hours upon hours and every answer I come across whenever I google anything doesn't actually explain what I am doing wrong, or I just am not reading it properly. 由于我是崭新的,我一直在寻找答案,而且每个小时都在搜索,每当我在google上搜索到的任何内容都不能真正解释我在做什么的时候,或者只是阅读得不正确。

The error on line 22 is caused by the function Player being of type object and you don't return an object in the method. 第22行的错误是由于Player函数的类型为object而导致的,您没有在该方法中返回任何对象。 If you replace: 如果您更换:

public object Player(PlayerMovement)

with: 与:

public void Player(PlayerMovement)  

That error should go away or you could return an object but I am not sure what that would be. 该错误应该消失,或者您可以返回一个对象,但是我不确定那会是什么。

First of all, as Spirit carp said. 首先,如精神鲤鱼所说。 the program will expecting a return value so you should either return a value or change object to void 程序将期望返回值,因此您应该返回一个值或将对象更改为void

public object Player....

into a 变成一个

public void Player....

and then the main problem is you are invoking the Transform.Translate instead of changing the actual transform of your gameObject. 然后主要的问题是您正在调用Transform.Translate而不是更改gameObject的实际变换。 as you said the PlayerMovement script is attached on your player gameobject so the goal is to make the gameobject change it's transform depends on the key input. 就像您说的那样,PlayerMovement脚本已附加在您的玩家游戏对象上,因此目标是使游戏对象发生变化,其变换取决于按键输入。 try to invoke the transform itself not the class Transform. 尝试调用转换本身而不是转换类。 transform is the actual transform of the gameobject and Transform is the class so you are invoking an static method, instead try to use this.transform.Translate transform是游戏对象的实际转换,而Transform是类,因此您在调用静态方法,请尝试使用this.transform.Translate

this.transform.Translate(Vector3.forward * Time.deltaTime);

then the last thing make sure to call your method inside Update method to make it works 然后最后一件事,请确保在Update方法中调用您的方法以使其起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM