I have lots of objects with a script attached, and I want each instance of the script to create a static variable based on the name of the gameObject it’s attached to and the variable to contain the coordinates of the object. Can this be done in a simple way?
Also, is it possible to check from another script whether a variable of name X exists and if so, read the coordinates from it?
Noob question perhaps, but I can’t find an answer by searching.
Any help appreciated!
Edit: I’m using JavaScript.
i don’t understand why you are not simply using a public variable that has the same name on all objects
Hi dbp.
Because I need to access the variable from another script attached to another gameobject. Maybe I’m missing something, I’m still pretty new at this
Look at the sections ‘Accessing other Components’ and ‘Accessing other Game objects’ on the front page of the scripting documentation.
i’m guessing your problem is that you don’t have a reference to the gameObject in the other gameObject’s script, right?
I actually solved it in a more elegant way than the one I had in mind when I posted this. (And yes, I found the solution in “Accessing other game objects”, note to self: always check the front page of the scripting reference before posting stupid questions.) Thanks for your patience. 
Just for people stopping by, the solution he found was likely something like:
using UnityEngine;
using System.Collections;
public class MySpecialObject : MonoBehaviour
{
void Start ()
{
MySpecialObject[] objects = (MySpecialObject[])FindObjectsOfType (typeof (MySpecialObject));
MySpecialObject closest = this;
float minDistance = Mathf.infinity;
foreach (MySpecialObject currentObject in objects)
{
float distance = (camera.main.transform.position - currentObject.transform.position).magnitude;
if (minDistance > distance)
{
minDistance = distance;
closest = currentObject;
}
}
Debug.Log (closest.gameObject.name + " is the special object closest to the main camera - with a distance of only " + minDistance);
}
}