x


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.

more ▼

asked Mar 21 '12 at 09:11 AM

CookieBE gravatar image

CookieBE
2 1 1 2

Please post your code.

Mar 21 '12 at 09:13 AM DaveA
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
}
more ▼

answered Mar 21 '12 at 10:03 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

Similarly with deselecting, only with the removed objects.

Mar 21 '12 at 10:03 AM syclamoth

Thanks, it works perfect now! ;)

Mar 21 '12 at 10:11 AM CookieBE
(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:

x427
x161
x60

asked: Mar 21 '12 at 09:11 AM

Seen: 542 times

Last Updated: Mar 21 '12 at 10:11 AM