简体   繁体   中英

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.

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.

On line 29 I am getting An object reference is required for the non-static field, method or property '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.

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. 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

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. 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. 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

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

then the last thing make sure to call your method inside Update method to make it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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