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 /
  • Help Room /
avatar image
0
Question by Nano-Additcs · Dec 20, 2015 at 06:03 AM · transformobjecttransform.positionfindgameobjectswithtag

How do you convert an object to a transform?

Hi (and thanks)

I have a game where various objects orbit a planet. The following script is attached to the planet and I would like it to find all objects with tag rather than object name. Is there an elegant way to achieve this?

Currently the errors which appears are:

FindGameObjectWithTag can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene.

Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

UnityException: You are not allowed to call this function when declaring a variable.

Move it to the line after without a variable declaration.

If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. Planet..ctor () UnityEditor.DockArea:OnGUI()

 using UnityEngine;
 using System.Collections;
  
 public class Planet : MonoBehaviour
 {
     //private Transform GameObjectPlayerStart;
     //private Transform GameObjectPlayerGamePlay;
     private GameObject GameObjectPlayerStart = GameObject.FindGameObjectWithTag("GameObjectPlayerStart");
     private GameObject GameObjectPlayerGamePlay = GameObject.FindGameObjectWithTag("GameObjectPlayerGamePlay");
     public float gravitationalForce = 200;
     private Vector3 directionOfGameObjectPlayerStart;
     private Vector3 directionOfGameObjectPlayerGamePlay;
     private Transform b;
     private Transform c;
 
     void Start ()
     {
         directionOfGameObjectPlayerStart = Vector3.zero;
         directionOfGameObjectPlayerGamePlay = Vector3.zero;
         b = GameObjectPlayerStart.transform;
         c = GameObjectPlayerGamePlay.transform;
     }
 
     void FixedUpdate ()
     {
 
         directionOfGameObjectPlayerStart = (transform.position-b.position).normalized;
         GameObjectPlayerStart.GetComponent<Rigidbody2D>().AddForce (directionOfGameObjectPlayerStart*gravitationalForce);  
 
         directionOfGameObjectPlayerGamePlay = (transform.position-c.position).normalized;
         GameObjectPlayerGamePlay.GetComponent<Rigidbody2D>().AddForce (directionOfGameObjectPlayerGamePlay*gravitationalForce);   
         
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nano-Additcs · Dec 20, 2015 at 08:32 AM

So er... Two hours or so later and I (with a little help from the globalhypermeganet) have come up with a solution.

Posting it for anyone else who is trying to do something similar.

 using UnityEngine;
 using System.Collections;
 
 public class PlanetGravity : MonoBehaviour 
 {
     public float gravitationalForce = 344;
 
     private Vector3 directionOfGameObjectPlayerStart;
     GameObject[] players;
     
     void Start () {
         players = GameObject.FindGameObjectsWithTag("GameObjectPlayer");
         directionOfGameObjectPlayerStart = Vector3.zero;
     }
     
     void FixedUpdate () 
     {
         foreach(GameObject GameObjectPlayerStart in players) 
                 {
                 directionOfGameObjectPlayerStart = (transform.position-GameObjectPlayerStart.transform.position).normalized;
                 GameObjectPlayerStart.GetComponent<Rigidbody2D>().AddForce (directionOfGameObjectPlayerStart*gravitationalForce);  
                 }
     }
 }
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

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

Related Questions

Hey Guys How to make a script 'private Transform Waypoint;, 0 Answers

Snapping Objects 0 Answers

Change button after 'x' amount of time 2 Answers

I am trying to say if the Y of my player reaches below 0 change the players position to 0,1,0 it wont work thought and I tried using Vector3 but it says I cant use Vector3 like a method Please help! 1 Answer

How to set an object's position a certain distance away from a target at the same angle? 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