Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
AshwinTheGammer

How to make Camera and a GameObject to find its parent and child respectively.

Question

I have a gun script in which I have declared

public Camera weaponCamera;

Now I wanna assign the weaponCamera of gun script from its parent's parent's to its parent(MainCamera). Means like this FPSController>Lean(empty gameobject)>MainCamera(i wanna assign this)>Body_1(gameobject)>Arms(gameobject)>M4A1. FPS controller>Soldier. I dont wanna assign camera manually to inspector because I instantiate clone of m4 gameobejct. Similarly I have declared

public Gameobject SoldierModel;

I wanna assign this gameobject from FPSController's child to GameObject of GunScript.(public GameObject Soldier;)

I know the question is pretty hard(or may i dont know how to ask questions like this). I tried various codes like :

weaponCamera.parent = transform.parent.Find("MainCamera");

But it didnt work! Im stuck w/ it since Monday.


Don't make comments on my english. NVM!

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
Please login or register to see this code.

^ this is not actually a GameObject so you cant set the parent of the camera individually.

What you would have to do is either

1. Get the GameObject of the Camera and change the parent of that GameObject

You can get the GameObject like this:

Please login or register to see this code.

or

2. Create a copy of the Camera component (not so easy) and add it as a component of some gameobject and change the parent for that gameobject
 

but maybe someone else can help you as im not really good in Unity.

Share this post


Link to post
Share on other sites

  • 0

Parents parent is called the grandparent, then great grandparent, and so on. It's just family terms. A parent is transform.parent; A gandparent is: transform.parent.parent; a great grandparent is: transform.parent.parent.parent; and so on. Idk why your searching for a camera when you know where to find it.

 

another thing is, use the setparent method, not parent =. Please login or register to see this link.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...