- Home /
script optimization (choosing an object in game by click)
Hi everyone, i have little question. My script works fine but i dont really sure that this method is good.
function Update () { gameControl.worldpos = camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,0)); if (Input.GetMouseButtonUp(0)&&gameControl.worldpos.y<-0.5){ for (var i:int=0;i<fastCellArray.length;i++){ if (gameControl.worldpos.x>fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.x-0.5&&gameControl.worldpos.x<fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.x+0.5&&gameControl.worldpos.y>fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.y-0.5&&gameControl.worldpos.y<fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.y+0.5){ fastCellArray[i].GetComponent.<cellScript>().clicked=true;
}
}
}
}
Its choosing an object from array and make some actions with that object. Is it better than making click-test in each cell Object? (I've got permanent amount of cell Objects, that wont be changed, 27 objects)
Answer by Joshua · May 05, 2011 at 03:17 AM
Well, for future reference I'd replace
Vector3(Input.mousePosition.x,Input.mousePosition.y,0)
by Input.mousePosition. I also notice you're missing at least one "&" in your if() statement. The very first AND consists of only one "&" in stead of two.
Other then that and the extremely confusing if() statement it looks fine. I always prefer to have a single function iterate through it and see if you clicked it rather then adding a "click-test" on each Object. Although I guess it might be slightly faster, but for 27 objects I wouldn't worry.
What I meant with it 'looking confusing' is that you don't indent consistently, never use space and keep to much on a single line when you don't need to. To me, personally, it would look a lot clearer like this:
function Update () {
gameControl.worldpos = camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonUp(0)&&gameControl.worldpos.y<-0.5){
for (var i:int=0;i<fastCellArray.length;i++){
if ( gameControl.worldpos.x > fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.x - 0.5
&& gameControl.worldpos.x < fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.x + 0.5
&& gameControl.worldpos.y > fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.y - 0.5
&& gameControl.worldpos.y < fastCellArray[i].GetComponent.<cellScript>().selfTransform.position.y + 0.5 )
{
fastCellArray[i].GetComponent.<cellScript>().clicked=true;
}
}
}
}
tnx for your reply Joshua! What is so confusing about if()statement? And with what i can replace this statements?
Edited to show an example of how I would do it. $$anonymous$$now that this is just a matter of personal preference, but this makes it a lot easier for people who did not write the code to keep track of what is being executed in what order, and what is wrapped inside what statement.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131719im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Selecting One Piece, Moving to Selected Area 1 Answer
selected object in array lost in translation 0 Answers
Code optimisation - how to use arrays? 4 Answers
Merging together all GameObjects of an Array? 1 Answer
New Object Pooling Problem 1 Answer