Search Unity

Simple Texture Array

Discussion in 'Scripting' started by stevedarby02, Jan 28, 2014.

  1. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    I'm missing something very obvious from this script as it loads the texture in to all the variables rather than just the first empty one.

    Please can someone point me in the right direction?

    Thanks

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var textureArray : Texture[];
    5.  
    6. function Start ()
    7. {  
    8.  for (var i = 0; i < 5; i++)
    9.   {
    10.     textureArray [i] = Resources.Load("Test");
    11.   }
    12.  
    13. }
    14.  
     
  2. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
  3. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    I've looked at that already, but can't see how i change the new Texture2D(200, 100); for my Resources.Load("Test");?

    What is the correct syntax?

    Thanks
     
  4. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    i think you have to set a size before filling it
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var textureArray : Texture2D[];
    5.  
    6. function Start ()
    7. {  
    8.     var textureArray: Texture2D[] = new Texture2D[5]; // that is what i mean
    9.  
    10.     for (var i = 0; i < 5; i++)
    11.     {
    12.         textureArray [i] = Resources.Load("Test");
    13.     }
    14. }
    15.  
     
    Last edited: Jan 28, 2014
  5. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Sorry, that doesn't seem to work? it doesn't populate the texture

    Thanks
     
  6. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Anyone have any idea?

    Thanks
     
  7. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    For anyone coming across this with the same problem i had you simply need to add a 'return' to the code. So it only adds once and comes out of the code.

    Code (csharp):
    1.  
    2. var textureArray : Texture[];
    3.  
    4. function Start ()
    5.  
    6. {  
    7.     for (var i = 0; i < 5; i++)
    8.     {
    9.        textureArray [i] = Resources.Load("Test");
    10.        return;
    11.     }
    12. }
    13.  
    14.  
    15.  
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't understand why you would do that...if you just want to set the first texture in the array, there's no point using a loop at all. Making a loop and then returning after the first iteration is extra work for nothing.

    Code (csharp):
    1. textureArray[0] = Resources.Load("Test");
    --Eric