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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

A field initializer cannot reference the non-static field, method, or property 'Component.transform'

Discussion in 'Scripting' started by grip_choodle, Jan 9, 2021.

  1. grip_choodle

    grip_choodle

    Joined:
    Jan 6, 2021
    Posts:
    2
    Happy weekend, all!

    I'm trying to use a Vector3 to copy the x and z euler angles, then change the euler y angle to -180, for the 2D object attached to the script and all of its children. By doing so I hope to flip the objects across the y axis, making them face the opposite direction— left.
    I got the error in the title, however. Does anyone know how to make this work as intended without errors? I appreciate any insight, even if you're not too sure.


    The issue has only occurred after attempting to apply "transform.eulerAngles = FaceLeft" on line 72.
    The issue is on line 19, character 44, and character 80— Unity doesn't like the transform.eulerAngles.x & z.
    Here's my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterMovement : MonoBehaviour
    6. {
    7.     private void Start()
    8.     {
    9.         _rigidbody = GetComponent<Rigidbody2D>();
    10.     }
    11.  
    12.     //This is the movement speed
    13.     private static float MoveForce = 10;
    14.     private static float JumpForce = 8;
    15.     public Vector2 HorizontalVelocity = new Vector2(MoveForce,0);
    16.     public Vector2 VerticalVelocity = new Vector2(0, JumpForce);
    17.  
    18.     //This is the rotation for facing a sprite left.
    19.     private Vector3 FaceLeft = new Vector3(transform.localeulerAngles.x, -180, transform.localeulerAngles.z);
    20.  
    21.  
    22.     // </>
    23. //This is for the TpToSpawn method
    24.     public Vector3 SpawnPosition = new Vector3(-2, -3, 0);
    25.     public Vector4 Upright = new Vector4(0, 0, 0);
    26. // </>
    27.     private Rigidbody2D _rigidbody;
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.     private void Update()
    36.     {
    37.         //Move Player to spawn position, reset their velocity, and make them upright
    38.         //If they fall off the map
    39.      
    40.             if(transform.position.y < -5)
    41.             {
    42.                 Debug.Log("The Player should be respawning!");
    43.                 _rigidbody.velocity = new Vector2(0,0);
    44.                 transform.position = SpawnPosition;
    45.                 transform.eulerAngles = Upright;
    46.             }
    47.      
    48.         //This allows horizontal movement
    49.  
    50.         if(Input.GetButton("Right"))
    51.         {
    52.             _rigidbody.AddForce(HorizontalVelocity * Time.deltaTime, ForceMode2D.Impulse);
    53.         }
    54.  
    55.         if(Input.GetButton("Left"))
    56.         {
    57.             _rigidbody.AddForce(-HorizontalVelocity * Time.deltaTime, ForceMode2D.Impulse);
    58.         }
    59.      
    60.         //This allows for jumping if the character presses the jump button
    61.         //and their vertical velocity is less than 0.001 units/s
    62.         if(Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
    63.         {
    64.             //Add an Impulse force called JumpForce
    65.             _rigidbody.AddForce(VerticalVelocity, ForceMode2D.Impulse);
    66.         }
    67.  
    68.         //Makes the character face left when they move left
    69.         //changing the euler angles to the "FaceLeft" Vector3
    70.         if(Input.GetButtonDown("Left"))
    71.         {
    72.             transform.eulerAngles = FaceLeft;
    73.         }
    74.  
    75.    
    76.     }
    77. }
     
    Last edited: Jan 9, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You can't call functions in field initializers like you're doing in line 19.

    You can convert that variable into a getter pretty trivially and then it should work.

    NOTE: the "function" you're calling is
    transform.localeulerAngles
    , which is a getter in itself.

    ALSO:
    transform.localeulerAngles
    is misspelled; the Euler is capitalized.

    https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html
     
    grip_choodle likes this.
  3. grip_choodle

    grip_choodle

    Joined:
    Jan 6, 2021
    Posts:
    2
    Thank you very much! I'm glad you told me transform.localEulerAngles is a getter; I ended up deleting line 19 altogether and using this on line 80 INSIDE THE UPDATE METHOD which I previously failed to realize was the reason for that error:


    transform.eulerAngles = new Vector3(transform.eulerAngles.x, -180, transform.eulerAngles.z)
     
    Kurt-Dekker likes this.