name and possition of gameObjects in array

so I have and array with several game objects in it and i want to get the name and position of them when i click on them but cant seem to figure it out. i keep getting syntax errors or cant convert type to string. i am just trying to get this in a Debug.Log statement right now any help would be appreciated

Posting your attempted code and actual error is usually a good idea.

If the only problem is converting it to a string, then you probably just need to write .ToString() at the end of the variable you’re trying to print.

Yeah I kind of thought it would be a simple answer but i cant find a solution anywhere but here is some more dedail
i have an array of game objects

public GameObject[] tiles;

and i assign them like this

    private void Awake()
    {
        tiles = GameObject.FindGameObjectsWithTag("Tile");
    }

I have this scrip attached to a “player” that is instanciated on a button click,
and then i click on a “Tile” i want it to return the name and position by using this

    public void OnMouseUp()
    {
       Debug.Log(tiles.ToString());//this does nothing

    }

something like this works but obviously only on the one I specifically call

    public void OnMouseUp()
    {
       Debug.Log(tiles[22]);

    }

I feel like it should be something like this but this is not correct and I fell like i am just missing somthing

    public void OnMouseUp()
    {
       Debug.Log(tiles[] gameobject.transform.position);

    }

So there’s 2 ways of doing this. Neither of which requires an array, really.

  1. Individual Scripts.
    For this, you write a script that reacts to a click. It’s basically what you have there already. The output would look like this for the name:

Debug.Log(transform.gameObject.name);

“transform.gameObject” is the object the script is attached to, “.name” is the name attribute you want to display. For the position, it’s simply

Debug.Log(transform.position);

Since every object has the same script, but they don’t have the same name and position, every object, when clicked, will log something different.

  1. Raycast
    You can also use a raycast. To do so, you have an extra gameObject that deals with this. The script for the extra game object looks something like this:
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                Debug.Log(hit.transform.gameObject.name);
                Debug.Log(hit.transform.position);
            }
        }
    }

Now on every frame, this item will check if the left mousebutton is clicked and if so, will send out a ray into the world from the camera through the direction of the curser. If that ray hits something, it will log the item name and position. This especially makes things easier, since you can now add any number of items you like and it’ll still work the same way, not every object needs to have the script in it.

There are more elegant ways to catch the mouseclick, as well, but this should do for now.

Thanks for the reply. I Know I can do it in the ways you described. I was hoping to avoid using the Update() and making a separate script just cause I think further down the line that may cause problems. was just hoping do do it in the same script. You are right though It probably dont need to use the array but I figured that since i am already using the array for other things and it was there that it would be the esies way to accomplish my goals.

Just having a list of objects doesn’t inherently tell you which one you clicked on.

You could use an array to do something to all of them, or to check if some particular example is in the array or not. But if you want to single out one specific object, you still need some way of identifying the specific object that you want.

hmm, I guess that makes sense. thanks for the help

More to the point, the array allows you access to the tiles without them being clicked. You can do all kinds of stuff with that, EXCEPT checking them for being clicked.

But don’t be afraid of the Upgrade() solution. It’s just one object doing all the work here, it’s not all your objects updating all the time. That shouldn’t result in any problems.