|
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.
(comments are locked)
|
|
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;
}
}
}
}
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)
|
|
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! 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)
|

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?
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.