x


selecting prefabs

I have a control script attached to each of my prefabs and was wondering how to select each one individually and be able to control each one depending on which was selected.

more ▼

asked Jan 10 '12 at 06:19 AM

jessee03 gravatar image

jessee03
306 58 77 89

This is very unclear. Do you have a single control script that must work with several different objects, behaving according to the object selected? If this is true, is this script already attached to each prefab, or is it attached to another object and must control the instantiated game object?

Jan 10 '12 at 07:28 PM aldonaletto

pretty much the scene will have like 5 prefabs for example which each have the same exact script attached to it. Depending on which one is clicked is the one you can control. So if one is selected all the other ones are deselected, this enables you to only move the selected object.

Jan 10 '12 at 10:03 PM jessee03
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can use the same tag for all objects that have this script, then get them in an array at Start. When one of them is clicked, iterate through the array enabling the clicked object and disabling all the others (camera script):

var others: GameObject[];

function Start(){
  others = GameObject.FindGameObjectsWithTag("MyTag");
}

function Update(){
  if (Input.MouseButtonDown(0)){
    var hit: RaycastHit;
    var ray = camera.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit) && hit.transform.tag == "MyTag"){
      for (var obj: GameObject in others){
        var isMe = (obj==hit.transform.gameObject); // isMe is true only for the hit object
        obj.GetComponent(ControlScript).enabled = isMe;
      }
    }
  }
}
more ▼

answered Jan 10 '12 at 10:54 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

so would controlscript be the name of the script attached to the prefab ? Also would each prefab use the tag "MyTag" ?

Jan 10 '12 at 10:58 PM jessee03

Exactly: use the same tag in the objects to identify them, and replace ControlScript by the actual script name.

Jan 10 '12 at 11:04 PM aldonaletto

getting some error saying cannot convert gameObject to gameObject[]

Jan 10 '12 at 11:44 PM jessee03

Ooops! Mea culpa: it should be GameObject.FindGameObjectsWithTag, not FindWithTag! Answer edited.

Jan 10 '12 at 11:52 PM aldonaletto

My fault, again! hit.gameObject doesn't exist - it should be hit.transform.gameObject. Answer edited (again...)

Jan 11 '12 at 03:12 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

I believe that you basically must set all your prefabs control scripts as DISABLED on start.

Then whenever your 'activator' and 'deactivator' kick in (for example OnMouseDown and OnMouseUp) you just enable and disable the script back again!

more ▼

answered Jan 10 '12 at 09:42 PM

roamcel gravatar image

roamcel
1.2k 37 40 44

I just dont know how to disable all the other prefabs and enable only the one that was clicked.

Jan 10 '12 at 10:04 PM jessee03
(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:

x130

asked: Jan 10 '12 at 06:19 AM

Seen: 773 times

Last Updated: Jan 11 '12 at 03:40 AM