Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pixeye · May 05, 2011 at 03:11 AM · gameobjectarrayoptimizationclick

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)&amp;&amp;gameControl.worldpos.y&lt;-0.5){      

     for (var i:int=0;i&lt;fastCellArray.length;i++){

         if  (   gameControl.worldpos.x &gt; fastCellArray[i].GetComponent.&lt;cellScript&gt;().selfTransform.position.x - 0.5    
             &amp;&amp;  gameControl.worldpos.x &lt; fastCellArray[i].GetComponent.&lt;cellScript&gt;().selfTransform.position.x + 0.5    
             &amp;&amp;  gameControl.worldpos.y &gt; fastCellArray[i].GetComponent.&lt;cellScript&gt;().selfTransform.position.y - 0.5    
             &amp;&amp;  gameControl.worldpos.y &lt; fastCellArray[i].GetComponent.&lt;cellScript&gt;().selfTransform.position.y + 0.5 )                  

         {

             fastCellArray[i].GetComponent.&lt;cellScript&gt;().clicked=true;

         }               
     }           
 }       

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pixeye · May 05, 2011 at 03:25 AM 0
Share

tnx for your reply Joshua! What is so confusing about if()statement? And with what i can replace this statements?

avatar image Joshua · May 05, 2011 at 03:36 AM 0
Share

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.

avatar image Pixeye · May 05, 2011 at 03:46 AM 0
Share

oh! now I see. Your example is clean. Tnx!)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges