Search Unity

Error code CS1612...

Discussion in 'Documentation' started by RVDL_IT, Apr 15, 2017.

  1. RVDL_IT

    RVDL_IT

    Joined:
    Apr 7, 2017
    Posts:
    24
    I'm getting an error cade at lines (7-9,36) about the fact that a field initializer cannot reference the nonstatic field, method, or property.
    I tried to find the awnser over the internet but I tried everything but it all didn't work.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PickUp : MonoBehaviour {
    5.    
    6.     // The variables for the positions.
    7.     float CharacterPositionX = Character.transform.position.x + 3;
    8.     float CharacterPositionY = Character.transform.position.y + 3;
    9.     float CharacterPositionZ = Character.transform.position.z + 3;
    10.  
    11.     // The Player and the tagged objects.
    12.     public GameObject CarryableObject;
    13.     public GameObject Character;
    14.  
    15.     void Start() {
    16.         // The origin of the objects.
    17.         CarryableObject = GameObject.FindWithTag("Carryable");
    18.         Character = GameObject.Find("Character");
    19.         // The code for determining the position (Put in note mode to deactivate but keep it).
    20.         // CharacterPositionX = Character.transform.position.x + 3;
    21.         // CharacterPositionY = Character.transform.position.y + 3;
    22.         // CharacterPositionZ = Character.transform.position.z + 3;
    23.   }
    24.    
    25.     void OnTriggerStay(Collider other) {
    26.         if(Input.GetKeyDown("e")) {
    27.             // The position relative to the character.
    28.         CarryableObject.transform.position.x = CharacterPositionX;
    29.         CarryableObject.transform.position.y = CharacterPositionY;
    30.         CarryableObject.transform.position.z = CharacterPositionZ;
    31.         // The rotation relative to the character.
    32.         CarryableObject.transform.rotation = Character.transform.rotation;
    33.         // The gravity.
    34.         other.attachedRigidbody.useGravity = false;
    35.         }
    36.     }
    37. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287