• 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 Feb 20, 2015 at 07:07 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SBull · Feb 20, 2015 at 04:05 AM · javascripttransformparentpickupparent transform

Problem assigning transform variable to prefab.

I'm trying to have an object become parented to a character's bone when the character touches the object. I have to do it a bit differently than the typical method however, because my character is a prefab that is created when the scene starts. Therefore, I can't simply drag and drop the object in Unity to get it's transform. Instead I am trying to assign the transform in script, but the examples that I have found for this don't seem to work. Here is what I have so far:

 var item : GameObject;
 var itemTransform : Transform;
 var boneTransform : Transform;
 
 function Start()
 {
      itemTransform = transform.Find("Item");
      boneTransform = transform.Find("CharacterBone");
 }
 
 function OnTriggerEnter (theCollision : Collider)
 {
     if(theCollision.gameObject.tag == "Item")
     {
              itemTransform.parent = boneTransform;
         }
 }

This seems to work just if I assign the itemTransform and boneTransform by hand after the scene starts, but unfortunately that's not very convenient. Any ideas why the variables are not assigned on start?

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

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Feb 20, 2015 at 04:10 AM

Drag your prefab into scene (and zero it).

Assign your transform by dragging it.

Drag the whole object back into a space in the Project Manager to create a prefab that is now set up.

Transform.Find finds a child object on that transform.

If you want to search through all objects use GameObject.Find instead.

Comment
Add comment · Show 5 · 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 SBull · Feb 20, 2015 at 06:07 PM 0
Share

$$anonymous$$aybe I'm not understanding what you mean, but when I set up the prefab and drag it back into the project manager, it does not seem to set up the prefab. The GameObject and Transform variables are set to "None (GameObject)" or "None (Transform)".

I am trying to find a child object on the character so

 boneTransform = transform.Find("CharacterBone");

should work for that, but doesn't return anything.

And if I use

 itemTransform = GameObject.Find("Item");

it says that I can't convert a transform variable to a gameObject variable.

avatar image meat5000 ♦ · Feb 20, 2015 at 06:25 PM 0
Share

Ah sorry, I think I misunderstood you.

You use your OnTrigger to compare the tag of the item. Why not use the reference you have right there within that function to manipulate the item ins$$anonymous$$d?

 theCollision.transform.parent = 

transform.Find works for the transform the script is attached to and will not find Grandchildren.

avatar image SBull · Feb 20, 2015 at 06:32 PM 0
Share

I figured out that you can use

 itemTransform = gameObject.Find("Item").transform;

to get the transform of the gameObject. This solved my problem.

avatar image SBull · Feb 20, 2015 at 06:36 PM 0
Share

Using theCollision works too! Neat trick. Thanks for your help.

avatar image meat5000 ♦ · Feb 20, 2015 at 07:07 PM 0
Share

No problem

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

21 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

Related Questions

Problem with making child an object 2 Answers

Silly Instantiation Issue 0 Answers

Parent object is getting generated why ?? 1 Answer

making one object a parent of another via javascript 2 Answers

Object not Instantiating Correctly 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