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
1
Question by ChimpKing · Nov 27, 2010 at 09:14 PM · gameobjectlanguageconverting

Converting some C# pseudo code to UnityScript....

I was given some pseudo code for finding the right-most object in a game. Unfortunately it is in C#. Could anybody convert this for me so I can tweak it? Or can someone rewrite another code that serves the same purpose except in UnityScript.

Thanks.

CODE:

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;
    }
}


CURRENT CODE:

function Update() { var maxX = Mathf.NegativeInfinity; var farthest : GameObject;

for (current in gameObjectTypes) { var x = current.transform.position.x; if (x > maxX) { maxX = x; farthest = current; } } }

function allGameObjects() { var gameObjectTypes = FindObjectsOfType(GameObject); return;

}

ERROR: Unknown identifier 'gameObjectTypes'

Comment
Add comment · Show 9
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 Eric5h5 · Nov 28, 2010 at 02:00 AM 0
Share

Your allGameObjects function has to return a value. Also, it's unlikely you want that in Update, because FindObjectsOfType is somewhat slow.

avatar image ChimpKing · Nov 28, 2010 at 02:11 AM 0
Share

And how do I make my function "return" something? (Sorry I'm relatively new to coding)

avatar image Eric5h5 · Nov 28, 2010 at 02:17 AM 0
Share

Do FindObjectsOfType once and assign the result to a variable. You can update it if the number of objects changes. You can make a function return a value using "return", but since FindObjectsOfType is already a function that returns an array, making a function that does nothing but return FindObjectsOfType is just redundant.

avatar image ChimpKing · Nov 28, 2010 at 02:25 AM 0
Share

How would I call FindObjectsOfType once and then assign it to a variable? Like this? var gameObjectTypes = FindObjectsOfType(GameObject); If so, do I leave that var in the function allGameObjects function? Because then it can't read it because the var is local.

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

Updated Code again so you can see.

Show more comments

1 Reply

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

Answer by Eric5h5 · Nov 27, 2010 at 09:42 PM

var maxX = Mathf.NegativeInfinity;
var farthest : GameObject;
for (current in gameObjects) {
    var x = current.transform.position.x;
    if (x > maxX) {
        maxX = x;
        farthest = current;
    }
}

Edit: here's a fix for your code:

private var allGameObjects : GameObject[];

function Start () { allGameObjects = FindObjectsOfType(GameObject); }

function Update() { var maxX = Mathf.NegativeInfinity; var farthest : GameObject;

 for (current in allGameObjects) {
     var x = current.transform.position.x;
     if (x > maxX) {
         maxX = x;
         farthest = current;
     }
 }

}

Comment
Add comment · Show 8 · 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 ChimpKing · Nov 27, 2010 at 10:16 PM 0
Share

ERROR: $$anonymous$$ identifier: 'gameObjects'

avatar image Jesse Anders · Nov 27, 2010 at 10:22 PM 0
Share

You have to provide 'gameObjects'. We don't know what the context is, so we can't tell you what container to iterate over; that's up to you.

avatar image ChimpKing · Nov 27, 2010 at 10:33 PM 0
Share

What if I want it to check all the gameObjects? Would I do this: var allGameObjects = FindObjectsOfType(GameObject);

And then put "allGameObjects" in replace of 'gameObjects'?

avatar image ChimpKing · Nov 27, 2010 at 10:46 PM 0
Share

When I tried that I got: You are not allowed to call this function when declaring a variable.

avatar image Eric5h5 · Nov 28, 2010 at 12:09 AM 0
Share

You have to put the code inside a function.

Show more comments

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Creating an entity from code, and then communicating with it from Monobehaviours 1 Answer

Best practice for copying Components from one GameObject to another? 3 Answers

Setting unique data on a gameobject? 1 Answer

melee system script problem, what did i do wrong or what to do next? 0 Answers


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