- Home /
The question is answered, right answer was accepted
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[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//Convert each string number to a double.
double[] nums = new double[strings.Length];
for (int i = 0; i < strings.Length; i++)
{
nums [i] = Convert.ToDouble (strings [i]);
}
//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 [i];
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 [i];
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 [i]);
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!
Are you sure the script is attached to a gameobject ?
Does the script inherit from $$anonymous$$onoBehaviour ?
Don't make us guess... what is a CS0120 error? Please give the full message, and the full script. What method is this code block contained within?
@tanoshimi Sorry! I updated the question to include my full code and the full error message!
Answer by Hellium · Jun 06, 2016 at 07:14 AM
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.
Thanks! I had written the code in a Console program in order to test it and ensure the values were correct. When I brought it back into my Unity script, I forgot to change it!
Answer by imaxus · Jun 06, 2016 at 12:19 PM
Read the Unity 3D documentation is very clear and friendly :
http://docs.unity3d.com/ScriptReference/Transform.Rotate.html
https://docs.unity3d.com/ScriptReference/Transform.Translate.html
And as @Hellium mention there is no Main .
Answer by cadpeople · Jun 06, 2016 at 03:45 PM
Well, you can start by changing "public static void Main (string[] args)" to "void Update()" :)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
what does it mean when something does not exist in the current context mean 1 Answer
Logical Camera errors... 1 Answer
Cannot play a disabled audio source? 1 Answer