Use an objects (from array) position to focus a camera on

I have a list of buttons which is created by reading all the objects in an array. This works. But now I want to move the camera target to the transform.position of the object that is related to the clicked button. So if the label of the button says "ObjectX", I want to move the camera target to ObjectX.transform.position.

've been working with gameObject.Find and Transform.Find but I can not get it to work. The code below generates a nullReferenceException. I think I'm doing something wrong with gameObject.Find(toString).transform.position. Basically I need to find the object named as stored in toString (or CreateObjectList01.listOfObjects*) (I tried both into the gameObject.Find() but it didn't work) and get it's position coordinates which is a Vector3 I presume.

* *```* *for (var i = 0; i < CreateObjectList01.listOfObjects.length; i++)* *{* _var toString = "" + CreateObjectList01.listOfObjects*; //converts array objects to type string*_ _*if (GUILayout.Button(toString))*_ _*{*_ _*gameObject.Find("Cameratarget").transform.position = gameObject.Find(toString).transform.position;*_ _*}*_ _*}*_ _*```*_ _*

This compiles, but gives me the null reference exception when I click on one of the buttons. Your first suggestion doesn't solve it, the nullreference remains.*_ _*The second suggestion triggers the following error: "Assets/myGUI.js(84,114): BCE0019: 'transform' is not a member of 'Object'. "*_ _*I assume that means it's an array of Objects which is not the same as GameObjects. I did try that before therefore I tried to use the GameObject.Find instead. Is there perhaps a way to convert an Object to a GameObject?

*_ _*

Cameratarget exists and toString is not a GameObject but a string. If I try

*_ _*```*_ _var toString = CreateObjectList01.listOfObjects*; //object type*_ _*```*_ _*

instead of

_*```*_ _var toString = "" + CreateObjectList01.listOfObjects*; //string type*_ _*```*_ _*

I get an error :*_ _*BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.

*_ _*

Any help would be greatly appreciated!


Edit: The string conversion is done (together with string split) to get the object names in the button-labels without (unityengine.gameobject) in them. I tried both the "toString" as a string and object (like mentioned above. And also tried "CreateObjectList01.listOfObjects", neither worked, I think I'm doing very simple completely wrong, just can figure out what.


There is also blahblah.ToString() (ignoreing blahblah) which converts almost anything to a string, but don't you want Try CreateObjectList01.listOfObjects*.name


DaveA: I've implemented the ToString to create the string which works great! With the nullexception I have less luck I tried the following:

`GameObject.Find("Cameratarget").transform.position = GameObject.Find(CreateObjectList01.listOfObjects*).transform.position;`'* *this results in BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.

* *

Than I tried to get the name into a variable to use it later

* *```* _var tazer = CreateObjectList01.listOfObjects*.name;*_ _*```*_ _*

But this results in: BCE0019:'name' is not a member of 'Object'.

*_ _*

I also tried:

*_ _*```*_ _var tazer = CreateObjectList01.listOfObjects*;*_ _*GameObject.Find("Cameratarget").transform.position = GameObject.Find(tazer).transform.position;*_ _*```*_ _*

Which results in BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.


Do you have any clue? It seems to me that the array contains Objects and I need GameObjects, when I print them I get "objectname (UnityEngine.Transform)".


I just solved it: the names in toString contained a space at the end after the string splitting, but the object names do not end with a space. Therefore the objects could not be found. Thanks for your help!