• 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 /
avatar image
0
Question by brycem24 · Jul 10, 2014 at 07:48 PM · collisionmovementgameobjecttransformpostion

How to move a Game Object from a script not attached to it.

Hi I am making a Soccer 2D Game and I seem to have a problem with everyt$$anonymous$$ng I try to implement if you can see my former posts haha. But my newest problem is that I want it so when my net is collided with by the soccer ball (w$$anonymous$$ch will add 1 to the score) I want it to reset all of the characters positions. From what I understand the way to do that would be to use the transform function to change the position of the player and the ball. But since (from my very limited knowledge) I believe that you can only use the transform function if the script is attached to the player. But the script where the ball collides with the net is attached to the net. Please give me some insight on how to move the player to $$anonymous$$s starting location when the ball collides with the net (or when +1 is added to the score, whatever you believe would be easiest)

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 Tehnique · Jul 10, 2014 at 07:55 PM

You can use GameObject.Find("Player") to find the player object from the net. That returns a GameObject w$$anonymous$$ch has a transform component.

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 fueldown · Jul 10, 2014 at 08:18 PM 0
Share

using GameObject.Find() for anything in runtime is generally a bad idea as it is very performance intensive call. All you need to do is cache the player object in the editor (Note: This is only in case you are not generating the player object at runtime). Just add a public field and drag & drop the player object in the editor.

In case you have a player object instantiated with a prefab, you can do this instead. (Note that the player is cached in Start, so that you can use this reference to player later, without calling "find" every time you need that object.)

  public GameObject player;
     
     void Start(){
        // Use this if your player is a clone of a prefab //instantiated at runtime (Don't forget to tag your prefab).
        player = GameObject.FindGameObjectWithTag("Player");
     }





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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Layered Collisions 1 Answer

move camera when it collides with a trigger 1 Answer

Simultaneously moving GameObjects using C# script. 3 Answers

Player movement in the direction of rotation INACCURATE? 3 Answers

Is there any way to convert Collision to a Gameobject or Transform? 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