- Home /
Referencing children of an instantiated object.
var Possibility1:GameObject;
var Possibility2:GameObject;
var Possibility3:GameObject;
var Possibility4:GameObject;
var Possibility5:GameObject;
var Choice : GameObject;
var AppearDist:int = 50;
var x:int;
var Player : Transform;
var Real_Object : GameObject;
var a : boolean = true;
function Start ()
{
Player = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
if(a == true)
{
x = Random.Range(1.0, 5.0);
if(x==1)
{Choice = Possibility1;}
if(x==2)
{Choice = Possibility2;}
if(x==3)
{Choice = Possibility3;}
if(x==4)
{Choice = Possibility4;}
if(x==5)
{Choice = Possibility5;}
Real_Object = Instantiate(Choice, transform.position, Quaternion.identity);
a=false;
}
var dist = Vector3.Distance(Player.position, transform.position);
if(dist>AppearDist)
{
Real_Object.active = false;
}
else
{
Real_Object.active = true;
}
}
This script is supposed to create one of five objects at the place of an object it is attached to, and then calculate the distance between itself and the player. If the distance is big enough - disable the instantiated object. And then enable it again, when player gets close.
Problem is, it deactivates only the parent object, but not its children. I used the
for (var child : Transform in transform)
{
child.renderer.enabled = false;
renderer.enabled = false;
}
for previous version of a script, attached to the instantiated object itself (back then I just made it invisible). Is it possible to reference the children of the instantiated object in this case?
Problem is, I can not search by Tag or something like that, because there is a whole grid of these with a random width, length and hight, so referencing should be local.
Answer by valyard · Nov 02, 2012 at 01:36 PM
You should use GameObject.SetActiveRecursively.
Also deactivating a GameObject and disabling its renderer is not the same.
About the second part - I know) I'm not that hopeless...) I just posted it as an example of what I meant. Thank you for the answer!
Your answer
Follow this Question
Related Questions
Disable/Enable Script or Add/Remove? 1 Answer
static var for one object. 2 Answers
Children of DualTouchControls prefab keep being forced to active 0 Answers
Instantiate problem 1 Answer
Array problem -3 Answers