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 SBull · Feb 20, 2015 at 08:32 PM · javascripttransformprefabpickupmount

How to differentiate between prefabs on collision.

I have several character prefabs created one at a time. The prefabs move on their own through a stage. When a character collides with an object I need them to pick it up (it attaches to a mount in their hierarchy). I have this working just fine except for when the second prefab is created.

If the second prefab touches the object, it becomes attached to the first prefab's mount instead of the one that touched it. I'm not sure how I am supposed to tell the object which prefab is which. Here is the script I currently have.

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

Comment
Add comment · Show 2
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 lordlycastle · Feb 20, 2015 at 11:20 PM 0
Share

I'm guessing this script is attached to the character prefab, so it fires the OnTriggerEnter(Collider other), so ins$$anonymous$$d of using a variable just use other.transform.SetParent(transform). You have a transform variable referencing to an Item which is not needed. Also you should use GameObject.FindGameObjectWithTag("Item") (That's for C#, use the JS equivalent).

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

Oh yeah, whoops. I was using

 itemTransform.transform.parent = mountTransform;

but switched it the use theCollision and just forgot to remove that variable.

I'm a little confused on how to use the SetParent function. I looked at the documentation, but it doesn't really give an example of how to use it. Could you explain it a bit further or maybe point me to a tutorial that does? Sorry if I'm missing something obvious. This is still my first project.

1 Reply

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

Answer by SBull · Feb 23, 2015 at 08:17 PM

I figured out how to make it work. Using

 mountTransform = transform.Find("hierarchy/path/to/mount");

This was actually the first thing I tried, but couldn't get it to work because I put the entire path including the name of the prefab object. By dumb luck I randomly decided to drop the top level of the hierarchy and just start listing from the armature and it worked like a charm.

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

20 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

Related Questions

Instantiate prefab as child at a transform? (Javascript/Uniyscript) 2 Answers

Problem assigning transform variable to prefab. 1 Answer

Instantiated GameObject does not have variables 0 Answers

Saving customized transform in game 1 Answer

Why Transform and not Prefab? 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