- Home /
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'
Your allGameObjects function has to return a value. Also, it's unlikely you want that in Update, because FindObjectsOfType is somewhat slow.
And how do I make my function "return" something? (Sorry I'm relatively new to coding)
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.
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.
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;
}
}
}
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.
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'?
When I tried that I got: You are not allowed to call this function when declaring a variable.
Your answer
Follow this Question
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