• 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 ChimpKing · Nov 26, 2010 at 07:05 PM · gameobjectx-axis

Finding the Right-Most Object...

What would the script be like to find the gameObject that is farthest to the right. AKA the gameObject with the greatest x value. Could you provide an example script or atleast tell how? Thanks.

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 Jesse Anders · Nov 26, 2010 at 07:11 PM

Here's some pseudocode (untested):

float maxX = -float.Infinity;
GameObject farthest = null;
foreach (GameObject current in gameObjects) {
    float x = current.transform.position.x;
    if (x > maxX) {
        maxX = x;
        farthest = current;
    }
}

Another option would be to sort the objects by their x coordinates (using a custom comparator), and then choose the last in the list.

Comment
Add comment · Show 5 · 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 TowerOfBricks · Nov 26, 2010 at 07:53 PM 0
Share

Great code, but you could use $$anonymous$$athf.Infinity ins$$anonymous$$d. Not sure whether or not it matters, but anyway. To find all gameObjects in the scene you can use the FindObjectsOfType function.

avatar image Jesse Anders · Nov 26, 2010 at 08:53 PM 0
Share

It's actually pseudocode (as stated in my post), so 'float.Infinity' is just a placeholder for an appropriate language- or API-specific value. Granted, the code would almost compile as is (in C# at least), but nevertheless it's up to the OP to make adjustments as needed. (As for what to actually use, -$$anonymous$$athf.Infinity would work, as would float.NegativeInfinity, float.$$anonymous$$inValue, etc.)

avatar image ChimpKing · Nov 27, 2010 at 12:02 AM 0
Share

Could you make that pseudo code more javascript-like? I'm no good with C#. Also, what would I do with this part? "foreach (GameObject current in gameObjects) {" What does that mean?

avatar image Jesse Anders · Nov 27, 2010 at 01:23 AM 0
Share

I don't use UnityScript, so I'm afraid I'll have to leave converting the code for you to someone else. As for the 'foreach' part, it iterates over all the elements in a container, in this case, all the game objects in 'gameObjects' (where 'gameObjects' is a container of game objects that's been created elsewhere in the code).

avatar image ChimpKing · Nov 27, 2010 at 03:06 AM 0
Share

Ahhhhh... I will try to convert it to UScript myself then (or ask). Thanks for explaining the foreach part.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Hi everone! I am new to Unity so sorry if this question stupid. But basically can you help me fix the Code? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How Do I Make a 3D Axis Mover UI? 1 Answer

Any way to lock an object from changes in the editor? 4 Answers

Get a parent GameObject from a class on a script attached to a prefab,Getting a parent Gameobject from a component on a script attached to a prefab 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