• 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
1
Question by sketchers1 · Aug 11, 2012 at 01:00 AM · parentloadshooter

Auto Parent on Level Start

Hi everybody so I have a player named ASHOOTER and an object that stays throughout the game that I want to get parented to the ASHOOTER at the beginning of each level. I have this script but I don't believe it is correct because it doesn't work... Help?

    var shooter : GameObject;
 
 function Start () {
 shooter = GameObject.Find("ASHOOTER");
 transform.parent = shooter.transform.parent;
 }

Thanks in advance!

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
2
Best Answer

Answer by Muuskii · Aug 11, 2012 at 01:25 AM

So you want the GameObject named ASHOOTER to follow around the object that this script is on? If that's the case you want to use:

 function Start () {
 shooter = GameObject.Find("ASHOOTER");
 shooter.transform.parent = transform;
 }

If you wanted it the other way around you would reverse that last line into:

transform.parent = shooter.transform;

Transform.parent reference

Does this solve your issue?

Comment
Add comment · Show 4 · 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 sketchers1 · Aug 11, 2012 at 01:49 AM 0
Share

also is there a way to automatically move the object to ASHOOTER's coordinates and rotation when it gets parented?

avatar image Muuskii · Aug 11, 2012 at 03:25 AM 0
Share

I edited my answer to make it more clear, sorry about the confusion. In order to move to ASHOOTER's coordinates and rotation you would just copy Transform.position and Transform.rotation from the parent to the child. Note that you could also use Transform.localPosition which you would set to (0,0,0)

avatar image Bunny83 · Aug 11, 2012 at 03:25 AM 0
Share

@sketchers1:
Sure, when it's parented you just need to set the localPosition to Vector3.zero and localRotation to Quaternion.identity.

avatar image sketchers1 · Aug 11, 2012 at 03:35 AM 0
Share

Well, since I had time what I ended up doing was attaching this script to ASHOOTER Prefab... so it still works out the same because of my DontDestroyOnLoad thing for the Sword Object. (By the way the Sword is the object... And since on each level the ASHOOTER's coordinates are different, how would I get the Sword Object to go to its coordinates then?

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

9 People are following this question.

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

Related Questions

How to load an item into the player's hand by clicking on a gameObject. 1 Answer

Big performance hit with assigning parent 0 Answers

Spawn inside of a parent 1 Answer

Make a simple tree 1 Answer

Possible to load image from desktop not url 3 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