• 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 Cbjfan1 · Jul 16, 2013 at 11:50 PM · errorgameobjecttransformparent

transform.parent can't go with gameObject.find

I'm trying to create a script where function update finds the main camera, then c$$anonymous$$lds the current object to it (The object the contains t$$anonymous$$s script), but I've run into a problem. If I change transform.parent to gameObject.parent, it finds the camera, but doesn't parent the object. If I do transform.find, the whole t$$anonymous$$ng doesn't work at all. Is there somet$$anonymous$$ng that I can use to replace gameObject that is compatible with transform.parent? Here is the script:

var cecked = gameObject.Find("Main Camera").GetComponent(HoldSend).holding;

 var cam = transform.Find("Main Camera");
 
 
 function Update () {
 cecked = gameObject.Find("Main Camera").GetComponent(HoldSend).holding;
 cam = transform.Find("Main Camera");
 }
 
 function OnMouseOver () {
 if(cecked == true){
 transform.parent = cam;
 }
 if(cecked == false){
 transform.parent = null;
 }
 }
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

3 Replies

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

Answer by save · Jul 17, 2013 at 12:12 AM

You should try to not find objects each frame, it's extremely resource heavy so try to move away from that type of habit. Instead cache your components. HoldSend could for instance be cached and accessed at any point.

Example:

 private var cameraTransform : Transform;
 private var cameraHoldSend : HoldSend;
 
 function Start () {
     cameraTransform = Camera.main.transform;
     cameraHoldSend = cameraTransform.GetComponent(HoldSend);
 }
 
 function OnMouseOver () {
     if (cameraHoldSend.holding)
         transform.parent = cameraTransform;
     else
         transform.parent = null;
 }

For the record you're also able to access the main camera through Camera.main, where you can access any derived components.

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
avatar image
1

Answer by aldonaletto · Jul 17, 2013 at 12:09 AM

transform.parent requires a Transform reference, and you should use GameObject.Find instead of gameObject.Find (GameObject is the class name, w$$anonymous$$le gameObject is a property). But it's much faster and easier to get the main camera reference from the variable Camera.main:

 function Update(){
   var cam: Camera = Camera.main; // get a Camera reference to the main camera
   cecked = cam.GetComponent(HoldSend).holding; // copy holding to cecked
   if (cecked){
     transform.parent = cam.transform;
   } else {
     transform.parent = null;
   }
 }

Better yet, if your main camera is always the same, save a reference to its script HoldSend at Start and use it in Update:

 private var holdSendScript: HoldSend; // reference to the HoldSend script
 
 function Start(){
   holdSendScript = Camera.main.GetComponent(HoldSend);
 }
 
 function Update(){
   if (holdSendScript.holding){
     transform.parent = Camera.main.transform; // c$$anonymous$$ld to the camera's transform
   } else {
     transform.parent = null;
   }
 }    

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
avatar image
0

Answer by ExplodingCookie · Jul 17, 2013 at 12:17 AM

You cannot assign an object variable in the initial declaration. Use t$$anonymous$$s script to hopefully solve your problem

 var cecked : HoldSend;
 var cam : GameObject;
 
 // Assign the variables
 function Start () {
     cam = GameObject.Find("MainCamera");
     cecked = cam.GetComponent(HoldSend);
 }
 
 function OnMouseOver () {
     if(checked.holding) {
         transform.parent = cam.transform;
     } else {
         transform.parent = null;
     }
 }

Hope t$$anonymous$$s solves your problem :)

~ExplodingCookie

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.

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

18 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

Related Questions

NaN exception when spawning soldiers 1 Answer

Properly Rotating Child Objects by Script 1 Answer

Deploy object avoiding collisions 0 Answers

Parenting from code doesn't really change the hierarchy 1 Answer

How to delay a line of code. 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