Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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 everything 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 (which 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 his 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 which 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 ins$$anonymous$$d. (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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[2D] Unsure why a gameObject is randomly falling through the floor instead of being transformed to the player 0 Answers

Modifying the Transform of a GameObject 1 Answer

can not move my enemy with rigidbody 1 Answer

Why does my character fly upwards when I click play? 1 Answer

Can't get gravity to work proberly (transform) 2 Answers


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