- Home /
Set only one child object visible
Hi everyone,
This is my first question on these boards.
I have a scene for a RTS game that is filled with units (just basic capsules). When I click a unit I wan't to set a plane visible that is attached as a child to the unit. All the child objects have the same name ("Plane"). The problem is when I select one unit all of the planes become visible.
The code:
#pragma strict
var selectedUnits = new Array();
var isSelected = false;
var target : Vector3;
private var selectableLayerMask = 1 << 9; //Units
private var walkableLayerMask = 1 << 10; //Ground
var hit : RaycastHit;
function Start () {
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit,200,selectableLayerMask)) {
Debug.Log("Clicked on a unit");
Debug.Log(hit.collider.name);
AddUnit(hit.collider.gameObject);
isSelected = true;
}
else {
Debug.Log("Clicked on the ground");
ClearSelectionList();
}
}
if (isSelected) {
transform.Find("Plane").renderer.enabled = true;
}
}
function AddUnit (unitToAdd : GameObject) {
selectedUnits.Push(unitToAdd);
GetComponent(MoveUnit).targetPosition = transform.position;
//transform.Find("Plane").renderer.enabled = true;
}
function ClearSelectionList () {
selectedUnits.Clear();
transform.Find("Plane").renderer.enabled = false;
isSelected = false;
}
Can someone help me with this issue.
Thanks in advance.
Answer by syclamoth · Mar 21, 2012 at 10:03 AM
Remove the lines:
if (isSelected) {
transform.Find("Plane").renderer.enabled = true;
}
and put them into 'AddUnit', with a small modification:
function AddUnit (unitToAdd : GameObject) {
selectedUnits.Push(unitToAdd);
GetComponent(MoveUnit).targetPosition = transform.position;
unitToAdd.transform.Find("Plane").renderer.enabled = true;
// This bit was almost right!
//transform.Find("Plane").renderer.enabled = true;
}
Similarly with deselecting, only with the removed objects.
Your answer

Follow this Question
Related Questions
Make a simple tree 1 Answer
How do I Toggle between alternatives? 3 Answers
Is object at least partly visible? 1 Answer
Turning on Visibility for Camera 2 Answers
Sprite not visible from behind 0 Answers