• 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
2
Question by KingKongFu · Dec 14, 2012 at 07:48 PM · cameraparent

Make a camera a child of a gameobject

Hello, so what im trying to do is make a camera a child of a game object. what I have is a perfab of a cube with its movement scripts and what not attached to it and also the camera harness gameObject which is just and empty gameObject. What I want to do when I spawn the perfab into the world I want the main camera to follow the cube. So I want it to be a child of the camera harness gameObject. I dont get any errors when I run this but the camera does not follow the cube when its moves. I dont know if I parented the camera right or not Here are the variables that are being used

var playerPrefab : GameObject;

var cam : Camera;

and here is where I parent I think.

 //spawn player prefab
 function spawnPlayer()
 {
     Network.Instantiate(playerPrefab, sp1.position, Quaternion.identity, 0);
     
     //camHolder is the parent
     // Get the transform of the camera
     cam.transform.parent = playerPrefab.GetComponent("CamHarness");
     
 }


Thanks for any help with this

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

Answer by Nidre · Dec 14, 2012 at 07:51 PM

There are two posible problem with this code.

  1. Are you sure if the "cam" object refers to the currently active camera.

  2. Are you sure that "playerPrefab.GetComponent("CamHarness");" actually return something.(it might be returning null which would mean cam wouldnt child of anything)

Comment
Add comment · Show 6 · 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 KingKongFu · Dec 14, 2012 at 08:08 PM 0
Share

Its null. I did parent the cam harness to the cube in the hierarchy pane by drag and dropping the cam harness onto the cube after I positioned it in the scene.

Is that not how you find children of a prefab?

avatar image Nidre · Dec 14, 2012 at 08:14 PM 0
Share

In your script you are basicly searching ro a Component named "CamHarness" you should look for gameobject or transform ins$$anonymous$$d.

Try this one :

playerPrefab.transform.Find("CamHarness");

avatar image KingKongFu · Dec 14, 2012 at 08:30 PM 0
Share

It gave me this error

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

Am I setting the parent wrong. It seams like im setting the cam harness as the child

avatar image Nidre · Dec 14, 2012 at 08:35 PM 0
Share

If you want change the parent of a tranform you should first detach it from the current parent. you can do this by.

cam.transform.parent = null; cam.transform.parent = playerPrefab.transform.Find("CamHarness");

avatar image KingKongFu · Dec 14, 2012 at 09:45 PM 0
Share

Ah I got it working thanks for all the help. It keep giving me null until I found the gameobject named camHarness like this

camHarness = GameObject.Find("CamHarness");

Debug.Log(camHarness.ToString());//this returns camHarness WOOO!!!

cam.transform.parent = camHarness.transform;

Show more comments

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

10 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

Related Questions

First Person Camera not being child of the character/ Set child's rotation to global rotation 2 Answers

How do I have a Child tell a Parent to move without the Child itself moving 0 Answers

How to activate parent camera of gameobject -1 Answers

How to Activate Children While Parent is Deactivated? 1 Answer

Unity2D button as child of camera not clickable 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