Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Read the positon of objects in an array

Discussion in 'Scripting' started by Raycat, Apr 5, 2011.

  1. Raycat

    Raycat

    Joined:
    Feb 17, 2011
    Posts:
    57
    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. Somebody has a tip?

    Code (csharp):
    1. for (var i = 0; i < CreateObjectList01.listOfObjects.length; i++)
    2. {      
    3.  var toString = "" + CreateObjectList01.listOfObjects[i];   //converts array objects to type string        
    4.                
    5.       if (GUILayout.Button(toString))    
    6.         {                            
    7.         gameObject.Find("Cameratarget").transform.position = gameObject.Find(toString).transform.position; 
    8.         }
     
    Last edited: Apr 5, 2011
  2. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    Does it compile like that? Find is a static function of the GameObject class. You should call it like this
    Code (csharp):
    1. GameObject.Find("Cameratarget").transform.position = GameObject.Find(toString).transform.position;
    Also, since you're always using the same Cameratarget object, you should probably just do the Find once on it in the Awake or Start function. I'm not sure if the CreateObjectList01.listOfObjects array is an array of GameObjects, but if it is you could also get rid of the second GameObject.Find by calling:
    Code (csharp):
    1. cachedCameraTarget.transform.position = CreateObjectList01.listOfObjects[i].transform.position;
    Either way, if you have a null reference exception, it would be because either "Cameratarget" does not exist in the scene, or the toString GameObject does not exist.
     
  3. Raycat

    Raycat

    Joined:
    Feb 17, 2011
    Posts:
    57
    Yes it compiles, it 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?

    BTW Cameratarget exists and toString is not a GameObject but a string. If I try
    Code (csharp):
    1. var toString = CreateObjectList01.listOfObjects[i];  //object type
    instead of
    Code (csharp):
    1. var toString = "" + CreateObjectList01.listOfObjects[i];  //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)'.
     
    Last edited: Apr 5, 2011
  4. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    It's hard to tell since I'm not sure what all of your variables are, but how do you populate the listOfObjects array? Anyway, if Cameratarget exists in the scene my best guess is that the "toString" variable does not have the same name as a GameObject in the scene. GameObject.Find only searches through GameObjects that are in the current scene.
     
  5. Raycat

    Raycat

    Joined:
    Feb 17, 2011
    Posts:
    57
    If I put a Print just before the line that gives the error; toString is "car (UnityEngine.Transform)", car being the object name.
    Thus toString actually contains something but it seems that it cannot find a GameObject for it.
    Also this script is attached to the parent GameObject, so all the objects (in toString) are located in this parent.
     
  6. Raycat

    Raycat

    Joined:
    Feb 17, 2011
    Posts:
    57
    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!