- Home /
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;
}
}
Answer by Berenger · May 11, 2012 at 03:36 AM
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 !)
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).
i tryed the script but it dosnt work i want to disable the camera anyone know how
Script Reference
If that doesn't help you convert this ONE line yourself, then I'd hire a programmer.
Or, do the smart thing, and buy all your program$$anonymous$$g assets from the Asset store. It's actually much cheaper than hiring someone to work for you constantly.
Answer by syclamoth · May 11, 2012 at 03:59 AM
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.
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 instan$$anonymous$$ted) called Camera so why isnt it working
What have you set 'bullit' to? I'm assu$$anonymous$$g that 'bullit' is the newly instantiated object.