How to Rotate/Translate a GameObject via C# Script

So, I’m writing a script to move my gameObject (basically a submarine). I’m collecting the x/z coordinates as well as my rotation from a text file. In my code, I convert the lat, long and heading coordinates to meter distances and change in rotations.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Text;
 using System.IO;
 using System.Collections;
 using System;

 public class PlayerController: MonoBehaviour {

  public GameObject remus;
  private Vector3 position;

  public static void Main (string[] args)
  {

	float speed = 1.0f;

	//Import text file and read all.
	string text = System.IO.File.ReadAllText("/Applications/Unity/Projects/Underwater/SampleFile.txt");
	if (text != null) {

		//Split the string of text into an array containing each number in the file.
		string[] strings = text.Split (new[] { " ", "

" }, StringSplitOptions.RemoveEmptyEntries);

		//Convert each string number to a double.
		double[] nums = new double[strings.Length];
		for (int i = 0; i < strings.Length; i++) 
		{
			nums  _= Convert.ToDouble (strings *);*_

* }*
* //Common variables*
* double R = 6371000;*
* int Entries = 86;*
* //Determine the Z distance travelled using the latitudes*
* double zDistances;*
* float zMeters;*
* for (int i = 0; i < Entries; i += 3)*
* {*
_ zDistances = nums [i + 3] - nums ;
zMeters = Convert.ToSingle(zDistances * R * Mathf.PI / 180);
* transform.Translate (0, 0, zMeters);
}
//Determine the X distances using the longitudes*
* double xDistances;
double lat;
float xMeters;
for (int i = 1; i < Entries; i += 3)
{
xDistances = nums [i + 3] - nums ;
lat = nums[i-1];
xMeters = Convert.ToSingle(R * (xDistances) * (Mathf.PI / 180) * Math.Cos(lat));
transform.Translate (xMeters, 0, 0);
}*_

* //Determine change in heading*
* float rotation;*
* for (int i = 2; i < Entries; i += 3)*
* {*
_ rotation = Convert.ToSingle(nums [i + 3] - nums );
* transform.Rotate(0, rotation, 0);
}
}
}*

My issue occurs with the 3 "transform. " in each if statement. I am getting this error message:_

> /Applications/Unity/Projects/Underwater/Assets/Scripts/PlayerController.cs(5,5): Error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform’ (CS0120) (Assembly-CSharp)
and I’ve checked out so many other questions that ask about this error and none of them seem to help my issue. I have tried using capital and lowercase letters, .rotation and .translation, and I still get them same error. I’m wondering what I’m doing wrong or if there is a better way to go about it.
Thanks in advance!

The problem is simple :

 public static void Main (string[] args)

There is no “Main” function in Unity. Unity is based on component behaviours, and there is no “entry point” of your program such as traditionnal programs.

Since its a static function, you can’t access the properties of the object the script is attached to. I guess, you can change Main for the Awake or Start method.

Read the Unity 3D documentation is very clear and friendly :

And as @Hellium mention there is no Main .

Well, you can start by changing “public static void Main (string args)” to “void Update()” :slight_smile: