Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 06, 2016 at 05:04 PM by KatrinaMay14 for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by KatrinaMay14 · Jun 05, 2016 at 09:08 PM · c#error messagetransform.translatetransform.rotate

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 05, 2016 at 05:25 PM 0
Share
  1. Are you sure the script is attached to a gameobject ?

  2. Does the script inherit from $$anonymous$$onoBehaviour ?

avatar image KatrinaMay14 · Jun 05, 2016 at 05:36 PM 0
Share

@Hellium Yes and yes :)

avatar image tanoshimi · Jun 05, 2016 at 09:57 PM 0
Share

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?

avatar image KatrinaMay14 · Jun 05, 2016 at 11:31 PM 0
Share

@tanoshimi Sorry! I updated the question to include my full code and the full error message!

3 Replies

  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KatrinaMay14 · Jun 06, 2016 at 05:06 PM 0
Share

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!

avatar image
2

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 .

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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()" :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges