x


how to get the child of an instanteated object

i have a rocket that i create and there is a camera inside of it i want to be able to enable and disable that camera

       if (GUI.Button(Rect(450,280,80,80),"Rockets"))
    {


         var bullit = Instantiate(rocketpref, GameObject.Find("rs").transform.position, Quaternion.identity);




    }
    if (GUI.Button(Rect(350,280,80,80),"cam"))
    {
       var newcam = bullit.Find("rocket/Camera").camera; 
       //var child = GameObject.Find("/Camera").camera;       
       if(newcam){ print("found");}
       //newcam.depth=1;

    }
}
more ▼

asked May 11 '12 at 03:25 AM

zeuo gravatar image

zeuo
373 52 70 83

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If the camera is a child of bullit, you can access that component that way :

var cam : Camera = bullit.GetComponentInChildren.< Camera >();

(Edit : I usually program in C# but I try to write things in javascript here. But I mixed them both here, sorry about that !)

more ▼

answered May 11 '12 at 03:36 AM

Berenger gravatar image

Berenger
11k 12 19 53

can u put that in java please i dont know anything about c#

May 11 '12 at 03:48 AM zeuo

I'm pretty sure java won't help you here, it's not a language that Unity supports. In any case, the syntax in Java is virtually the same as in C#... In any case, you should learn C#. It's not actually any more complex than UnityScript (your programs will still need to work the same way), and it is a generally more useful language (because it's not a unique JavaScript variant that is only used in one place).

May 11 '12 at 03:53 AM syclamoth

i tryed the script but it dosnt work i want to disable the camera anyone know how

May 11 '12 at 03:56 AM zeuo

Script Reference
If that doesn't help you convert this ONE line yourself, then I'd hire a programmer.

May 11 '12 at 03:56 AM Lo0NuhtiK

Or, do the smart thing, and buy all your programming assets from the Asset store. It's actually much cheaper than hiring someone to work for you constantly.

May 11 '12 at 04:00 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

Code! To disable a camera that is the child of an object! In both UnityScript and C#!

var childCam : Camera = bullit.GetComponentInChildren.<Camera>();
if(childCam != null)
{
    childCam.enabled = false;
}

Camera childCam = bullit.GetComponentInChildren<Camera>();
if(childCam != null)
{
    childCam.enabled = false;
}

If you can't tell which is which, you need to do a little more homework.

more ▼

answered May 11 '12 at 03:59 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

ty but i got Object reference not set to an instance of an object i have a camera inside of the rocket (which is being instanteated) called Camera so why isnt it working

May 11 '12 at 04:03 AM zeuo

What have you set 'bullit' to? I'm assuming that 'bullit' is the newly instantiated object.

May 11 '12 at 02:18 PM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3001
x410
x134
x74

asked: May 11 '12 at 03:25 AM

Seen: 865 times

Last Updated: May 11 '12 at 04:02 PM