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

What is going wrong with my code here

Discussion in 'Scripting' started by Jhondotcom, Jun 29, 2020.

  1. Jhondotcom

    Jhondotcom

    Joined:
    Jun 28, 2020
    Posts:
    2
    Error Messages I am getting
    Assets\Scripts\Camera_Movement.cs(21,13): error CS0120: An object reference is required for the non-static field, method, or property 'Transform.position'
    Assets\Scripts\Camera_Movement.cs(24,47): error CS0120: An object reference is required for the non-static field, method, or property 'Transform.position'

    Code I am using
    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using UnityEngine;

    public class Camera_Movement : MonoBehaviour
    {

    public Transform target;
    public float smoothing;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void LateUpdate()
    {
    if (Transform.position != target.position)
    {
    Vector3 targetPosition = new Vector3(target.position.x, target.position.y, target.position.z);
    transform.position = Vector3.Lerp(Transform.position, targetPosition, smoothing);
    }
    }
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    1. Use code tags when posting code, that way it'll have line numbers (in addition to not mangling certain syntax and just being easier to read overall)

    2. Transform.position should be transform.position. Capital T is for the class itself, lowercase t is for the transform instance attached to this object.
     
  3. Jhondotcom

    Jhondotcom

    Joined:
    Jun 28, 2020
    Posts:
    2
    ok thanks for your help next time I will use a code tag