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

An Object reference is required for the non static field....?

Discussion in 'Scripting' started by herold101, Jan 10, 2015.

  1. herold101

    herold101

    Joined:
    Jan 20, 2014
    Posts:
    22
    ...method or property. Hey all. I have been wondering about making a system where my '2D' sprite character walks up, down, left, right and so on on a grid. I have made a little script and I get the same issue after trying many different methods.

    This is the part of code the error apears on:

    Code (CSharp):
    1. Transform.position += Vector3.right;
    2.  
    3. Transform.position += Vector3.left;
    4.  
    5. Transform.position += Vector3.down;
    6.  
    7. Transform.position += Vector3.up;
    This is the entire code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChracterControler : MonoBehaviour {
    5.     public float snapValue = 1;
    6.     public float depth = 0;
    7.    
    8.     void Update(){
    9.  
    10.         float x, y, z;
    11.  
    12.                 if (Input.GetKey ("w"))
    13.                         Transform.position += Vector3.up;
    14.        
    15.                
    16.         if (Input.GetKey ("a"))
    17.                         Transform.position += Vector3.left;
    18.  
    19.            
    20.         if (Input.GetKey ("d"))
    21.                         Transform.position += Vector3.right;
    22.  
    23.                
    24.         if (Input.GetKey ("s"))
    25.                         Transform.position += Vector3.down;
    26.  
    27.                
    28.         float snapInverse = 1 / snapValue;
    29.  
    30.         x = Mathf.Round(transform.position.x * snapInverse) / snapInverse;
    31.             y = Mathf.Round(transform.position.y * snapInverse) / snapInverse;
    32.                 z = depth;
    33.  
    34.  
    35.  
    36.  
    37.  
    38.         }
    39. }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Make the T on Transform lower case.
     
    herold101 likes this.
  3. herold101

    herold101

    Joined:
    Jan 20, 2014
    Posts:
    22
    I did it and it works which is awesome. However when I add the script to a gameobject it says the script does not exist. :p
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Make sure the filename matches the class name
     
    herold101 likes this.
  5. herold101

    herold101

    Joined:
    Jan 20, 2014
    Posts:
    22
    Fixed thanks, Does not work the way I had it in theory but I will figure that out eventually. Thank you all!