Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Movement Script Error

Discussion in 'Scripting' started by Diliard, Jul 27, 2020.

  1. Diliard

    Diliard

    Joined:
    Jul 27, 2020
    Posts:
    2
    Hey people, I tried to create a Simple Movementscript and it worked but as soon as i moved the checks out of FixedUpdate and moved them to their respective booleans my code broken and i don't know why. Help would be appreciated.
    Error: "
    UnityException: GetKeyString is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'PlayerMovement' on game object 'Player'.
    See "Script Serialization" page in the Unity Manual for further details."
    I checked The page in the Manual and i didn't find anything useful.
    Code:
    Code (CSharp):
    1. /* using System.Collections;
    2. using System.Collections.Generic; */
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     public float Xforce = 750f;
    7.     public float Yforce = 750f;
    8.     public float Zforce = 750f;
    9.  
    10.     static bool Z;
    11.     static bool NegX;
    12.     static bool NegZ;
    13.     static bool X;
    14.  
    15.     public Rigidbody rb;
    16.     // Start is called before the first frame update
    17.     void Start() {
    18.         Debug.Log("Initializing PlayerMovement");
    19.     }
    20.     // Update is called once per frame
    21.     void Update() {
    22.         if(Input.GetKey("w")) {
    23.             Z = true;
    24.         }/*else if(NegX == true) {
    25.             rb.AddForce(-Xforce * Time.deltaTime, 0, 0);
    26.         }else if(NegZ == true) {
    27.             rb.AddForce(0, 0, -Zforce * Time.deltaTime);
    28.         }else if(X == true) {
    29.             rb.AddForce(Xforce * Time.deltaTime, 0, 0);
    30.         }*/
    31.  
    32.        /*Z == Input.GetKey("w");
    33.        NegX == Input.GetKey("a");
    34.        NegZ == Input.GetKey("s");
    35.        X == Input.GetKey("d");
    36.        */
    37.     }
    38.     // FixedUpdate is called once per frame (Physics Calculation)
    39.     void FixedUpdate() {
    40.  
    41.  
    42.         if(Z == true) {
    43.             rb.AddForce(0, 0, Zforce * Time.deltaTime);
    44.         }else if(NegX == true) {
    45.             rb.AddForce(-Xforce * Time.deltaTime, 0, 0);
    46.         }else if(NegZ == true) {
    47.             rb.AddForce(0, 0, -Zforce * Time.deltaTime);
    48.         }else if(X == true) {
    49.             rb.AddForce(Xforce * Time.deltaTime, 0, 0);
    50.         }
    51.  
    52.     }
    53. }
    54.  
     
  2. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    This line should be
    if(Input.GetKey(Keycode.W)

    Same for the other 'GetKeys'.Or you could just use
    if(Input.GetButton("w"))
    but you have to register it from Project Setting > Input.
     
  3. Diliard

    Diliard

    Joined:
    Jul 27, 2020
    Posts:
    2
    Thank you!

    You definitly Helped me out, it lead me to the right path to fix it.

    It turned out that everything magically fixed itself after i rewrote the entire Update function. Weird.

    (Your help made the code before work too excep that it permanently Stayed at true. which actually was a Mistake i made, But after i rewrote the entire function to just assigning the Variable the function (( Example:
    Z = Input.GetKey(Keyode.W);
    Magicaclly worked after i rewrote the entire function.)
     
    Hiktses likes this.