Search Unity

Can't get Array of gameObjects working

Discussion in 'Scripting' started by roto23, Mar 24, 2012.

  1. roto23

    roto23

    Joined:
    Oct 30, 2009
    Posts:
    240
    It crashes on the last line with the "object variable not set to an instance of an object". However I do see the object in game and I can move it by changing its x value in the editor. It crashes on the first loop.

    Code (csharp):
    1.  
    2. GameObject[] goTC = new GameObject[12];
    3. Vector3 v3 = new Vector3(.5f,.5f,2f);
    4. for(int i=0; i<12; i++)
    5. {  
    6.     goTC[i] = Instantiate(tfTC,v3,Quaternion.identity) as GameObject;
    7.     Vector3 vec3 = new Vector3(.1f,.1f,2f);
    8.     goTC[i].transform.position = vec3;
    9. }
    10.  
     
  2. roto23

    roto23

    Joined:
    Oct 30, 2009
    Posts:
    240
    Well, I switched from an array to a LIST and getting the same error. When I use a LIST it creates all 12 and I query and loop through and a counter counts 12 of them, however, when I try to read its transform.position property, I get the error.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You must have something going on elsewhere, because the code you posted works without errors. Although I don't see why you're instantiating at one position, and then immediately changing it to a second position; it would be simpler if you instantiated at the second position to begin with. Also you'd do yourself a big favor by using descriptive variable names.

    --Eric
     
  4. roto23

    roto23

    Joined:
    Oct 30, 2009
    Posts:
    240
    yah, I have some sneaky error somewhere. I dumped the code into VS2010 express and it works, and it also works in another Unity project of mine. Me instantiating one place and then right away moving it to another place is not really want I want to do. I just threw that in there to show I get the Object ref error.

    But thanks for the effort.
     
  5. roto23

    roto23

    Joined:
    Oct 30, 2009
    Posts:
    240
    man, I'm a moron......today. Some days I do real good though. So stupid was the error.. what I was instantiating was not even a gameObject, once I made it a gameObject it worked. So, there was no way you could trouble shoot it with the code I provided.