Search Unity

Problem with intantiating arrays in script

Discussion in 'Scripting' started by LePaithon, Feb 11, 2016.

  1. LePaithon

    LePaithon

    Joined:
    Feb 24, 2015
    Posts:
    5
    So i have been working on my script and i have set the position of an array to another gameobject, but when i run my game , it is set not till the end of my canvas corner ( i have looked every object and they are set to 0 or on the right position) . I decided that i must use whole array to set that position , because i have have tried to set the parent object to that position before it must be instantiated and it was set not to 0 of that gameobject position.
    Here's the script:
    Code (CSharp):
    1.            public GameObject[] cubes;
    2.            public GameObject Cloness;
    3. .....
    4.         Camera mainCamera = GetComponent<Camera>();
    5.         Cloness.transform.localPosition = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0));
    6.         for (int y = 0; y < gridY; y++)
    7.         {
    8.             Vector3 pos = new Vector3(Cloness.transform.position.x, y, 0) * spacing;
    9.             GameObject mySoundLine = Instantiate (prefab, pos, transform.rotation)as GameObject;
    10.             mySoundLine.transform.parent = Cloness.transform;
    11.         }
    12.         cubes = GameObject.FindGameObjectsWithTag("cubes");
    Thank you for the help already ^^
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Could you try asking the question again? I wasn't able to figure out what it is you need. Can you describe what exactly you are trying to accomplish?

    Neither of these sentences makes any sense to me.

    One piece of advice off the bat - I am guessing that, by the end of your code snippet, 'cubes' should contain all the things you just instantiated? If so, use this instead of FindObjectsWithTag:
    Code (csharp):
    1. cubes = new GameObject[gridY];
    2. for (int y = 0; y < gridY; y++) {
    3. GameObject mySoundLine = Instantiate(blah blah);
    4. cubes[y] = mySoundLine;
    5. }
    It's faster, the cubes will be in a predictable order in the array, and it won't ever find stray tagged objects that might be laying around in the scene.
     
    LePaithon likes this.
  3. LePaithon

    LePaithon

    Joined:
    Feb 24, 2015
    Posts:
    5
    What i really want to say is that i want to move my whole array with child object in another position ( it is in the corner of the screen ) and i don't know how to do that . Without FindObjectsWithTag game is not working .