Search Unity

[SOLVED] Hi can anyone help me I don't know if I'm making my texture correctly

Discussion in 'Scripting' started by McSpiffy, Nov 7, 2014.

  1. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    Hi was going back through my old script to make it more efficient and want to get rid of the 9 sprite renderers I had on my gameobject and make them into 1. This also meant getting the 9 sprites and turning them into one.
    So i tried converting them into a texture and then applying the texture in to a sprite to use.
    the problem is I don't know if i'm making the texture correctly.You see I'm build walls on the fly from a top-down perspective and if a wall touches another wall the Image need to change so the walls can flow together.
    Note: I'm new at this and I am trying to learn.

    Code (CSharp):
    1. sprites[0]  = bottomLeft.texture;
    2.         sprites[1]  = bottomCenter.texture;
    3.         sprites[2]  = bottomRight.texture;
    4.         sprites[3]  = centerLeft.texture;
    5.         sprites[4]  = centerCenter.texture;
    6.         sprites[5]  = centerRight.texture;
    7.         sprites[6]  = topLeft.texture;
    8.         sprites[7]  = topCenter.texture;
    9.         sprites[8]  = topRight.texture;
    10.  
    11.  
    12.  
    13.         int x,y;
    14.         int xx = 96;
    15.         int yy = 96;
    16.  
    17.         for(x = 0; x < xx;x++)
    18.         {
    19.  
    20.             for(y = 0; y < yy; y++)
    21.             {
    22.              
    23.  
    24.             }
    25.  
    26.             //bot
    27.             if(x<32)
    28.             {
    29.                 if(y<32)
    30.                 {
    31.                     Color[] a = sprites[0].GetPixels(0, 0, 32, 32);
    32.                     wallTexture.SetPixels(x ,y ,96,96, a);
    33.                     if(y>32 && y<64)
    34.                     {
    35.                         Color[] b = sprites[3].GetPixels(0, 0, 32, 32);
    36.                         wallTexture.SetPixels(x ,y ,96,96, b);
    37.                     }
    38.                     if(y>64 && y<96)
    39.                     {
    40.                         Color[] c = sprites[6].GetPixels(0, 0, 32, 32);
    41.                         wallTexture.SetPixels(x ,y ,96,96, c);
    42.                     }
    43.                 }
    44.  
    45.             }
    46.             if(x>32 && x<64)
    47.             {
    48.                 if(y<32)
    49.                 {
    50.                     Color[] d = sprites[1].GetPixels(0, 0, 32, 32);
    51.                     wallTexture.SetPixels(x ,y ,96,96, d);
    52.                     if(y>32 && y<64)
    53.                     {
    54.                         Color[]  e = sprites[4].GetPixels(0, 0, 32, 32);
    55.                         wallTexture.SetPixels(x ,y ,96,96, e);
    56.                     }
    57.                     if(y>64 && y<96)
    58.                     {
    59.                         Color[] f = sprites[7].GetPixels(0, 0, 32, 32);
    60.                         wallTexture.SetPixels(x ,y ,96,96, f);
    61.                     }
    62.                 }
    63.             }
    64.             if(x>64 && x<96)
    65.             {
    66.                 if(y<32)
    67.                 {
    68.                     Color[] g = sprites[2].GetPixels(0, 0, 32, 32);
    69.                     wallTexture.SetPixels(x ,y ,96,96, g);
    70.                     if(y>32 && y<64)
    71.                     {
    72.                         Color[]  h = sprites[5].GetPixels(0, 0, 32, 32);
    73.                         wallTexture.SetPixels(x ,y ,96,96, h);
    74.                     }
    75.                     if(y>64 && y<96)
    76.                     {
    77.                         Color[] i = sprites[8].GetPixels(0, 0, 32, 32);
    78.                         wallTexture.SetPixels(x ,y ,96,96, i);
    79.                     }
    80.                 }
    81.             }
    82.  
    83.         }
    84.  
    85.         wallTexture.filterMode = FilterMode.Point;
    86.         wallTexture.wrapMode = TextureWrapMode.Clamp;
    87.         wallTexture.Apply();
    88.  
    89.         wallRenderer.sprite = Sprite.Create(wallTexture, new Rect(0, 0, wallTexture.height, wallTexture.height), new Vector2(0.5f, 0.5f),96f);
    90.  
    I get this error
    Internal_Create can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
     
    Last edited: Nov 7, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I guess you have code outside functions. Do what the error says and put all code inside a function such as Awake or Start.

    --Eric
     
  3. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    Yea i tried to pull that code out and put it in the start still I get the same error
    I will put the full code up if you wish to see it. I still need to change the line casts to something more efficient tho.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. [RequireComponent(typeof(SpriteRenderer))]
    5. public class Wall : TileEntity
    6. {
    7.     private Texture2D wallTexture = new Texture2D(96,96);
    8.    
    9.     private Texture2D[] sprites;
    10.     private SpriteRenderer wallRenderer;
    11.     #region Composs Directions
    12.     // bools = true when another wall object is next to this 1 in the corresponding direction
    13.     public bool north;
    14.     public bool south;
    15.     public bool east;
    16.     public bool west;
    17.     public bool northEast;
    18.     public bool southEast;
    19.     public bool northWest;
    20.     public bool southWest;
    21.     #endregion
    22.    
    23.     #region Tile Sprites
    24.     // define each image 0-13  \\\\\\\\\\\\\\o.O///////////////
    25.     public Sprite topLeftOutCorner;
    26.     public Sprite topLeftInCorner;
    27.     public Sprite topCenterWall;
    28.     public Sprite topRightOutCorner;
    29.     public Sprite topRightInCorner;
    30.     public Sprite centerLeftWall;
    31.     public Sprite centerRoof;
    32.     public Sprite centerRightWall;
    33.     public Sprite bottomLeftOutCorner;
    34.     public Sprite bottomLeftInCorner;
    35.     public Sprite bottomCenterWall;
    36.     public Sprite bottomRightOutCorner;
    37.     public Sprite bottomRightInCorner;
    38.     #endregion
    39.    
    40.     #region Sprite Settings and Sprite Array
    41.     // define the 4 quadrant positions of the tile & the corresponding image
    42.  
    43.     //private SpriteRenderer[] spriteRen = new SpriteRenderer[9];
    44.    
    45.     //private SpriteRenderer tLeft;
    46.     private Sprite topLeft;
    47.     //private SpriteRenderer tCenter;
    48.     private Sprite topCenter;
    49.     //private SpriteRenderer tRight;
    50.     private Sprite topRight;
    51.     //private SpriteRenderer cLeft;
    52.     private Sprite centerLeft;
    53.     //private SpriteRenderer center;
    54.     private Sprite centerCenter;
    55.     //private SpriteRenderer cRight;
    56.     private Sprite centerRight;
    57.     //private SpriteRenderer bLeft;
    58.     private Sprite bottomLeft;
    59.     //private SpriteRenderer bCenter;
    60.     private Sprite bottomCenter;
    61.     //private SpriteRenderer bRight;
    62.     private Sprite bottomRight;
    63.  
    64.  
    65.     #endregion
    66.    
    67.     #region RayCast Stuff
    68.     //
    69.     public Transform nStart, nEnd, sStart, sEnd , eStart, eEnd, wStart, wEnd, nEStart, nEEnd, sEStart, sEEnd, nWStart, nWEnd, sWStart, sWEnd;
    70.    
    71.     #endregion
    72.    
    73.     public bool buildMode;
    74.     void Start ()
    75.     {
    76.         wallRenderer= this.gameObject.GetComponent<SpriteRenderer>();
    77.         sprites = new Texture2D[9];
    78.  
    79.         onCreate();
    80.         onCreate();
    81.         onCreate();
    82.        
    83.        
    84.     }
    85.    
    86.    
    87.    
    88.     void Update ()
    89.     {
    90.        
    91.         if(buildMode == true)
    92.         {
    93.             onBuild();
    94.         }
    95.     }
    96.    
    97.     void onBuild()
    98.     {
    99.         onCreate();
    100.     }
    101.    
    102.    
    103.     void onCreate()
    104.     {
    105.         // Handels Sprite changes
    106.         /*
    107.         spriteRen = GetComponentsInChildren<SpriteRenderer>();
    108.         tLeft   = spriteRen[0];
    109.         tCenter = spriteRen[1];
    110.         tRight  = spriteRen[2];
    111.         cLeft   = spriteRen[3];
    112.         center  = spriteRen[4];
    113.         cRight  = spriteRen[5];
    114.         bLeft   = spriteRen[6];
    115.         bCenter = spriteRen[7];
    116.         bRight  = spriteRen[8];
    117.        
    118.         tLeft.sprite   = topLeft;
    119.         tCenter.sprite = topCenter;
    120.         tRight.sprite  = topRight;
    121.         cLeft.sprite   = centerLeft;
    122.         center.sprite  = centerCenter;
    123.         cRight.sprite  = centerRight;
    124.         bLeft.sprite   = bottomLeft;
    125.         bCenter.sprite = bottomCenter;
    126.         bRight.sprite  = bottomRight;
    127.         */
    128.  
    129.  
    130.        
    131.         //Debug.DrawLine(nStart.position, nEnd.position, Color.green);
    132.         north = Physics2D.Linecast(nStart.position, nEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    133.         south = Physics2D.Linecast(sStart.position, sEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    134.         east = Physics2D.Linecast(eStart.position, eEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    135.         west = Physics2D.Linecast(wStart.position, wEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    136.         northEast = Physics2D.Linecast(nEStart.position, nEEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    137.         southEast = Physics2D.Linecast(sEStart.position, sEEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    138.         northWest = Physics2D.Linecast(nWStart.position, nWEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    139.         southWest = Physics2D.Linecast(sWStart.position, sWEnd.position, 1 << LayerMask.NameToLayer("Walls"));
    140.        
    141.        
    142.         // North
    143.         centerCenter = centerRoof;
    144.         if (north == true)
    145.         {
    146.             topCenter = centerRoof;
    147.             #region Top Right and Center Right
    148.             if(northEast == true)
    149.             {
    150.                 if(east == true)
    151.                 {
    152.                     topRight = centerRoof;
    153.                     centerRight = centerRoof;
    154.                 }
    155.                 else
    156.                 {
    157.                     topRight = centerRightWall;
    158.                     centerRight = centerRightWall;
    159.                 }
    160.             }
    161.             else
    162.             {
    163.                 if(east == true)
    164.                 {
    165.                     topRight = topRightInCorner;
    166.                     centerRight = centerRoof;
    167.                 }
    168.                 else
    169.                 {
    170.                     topRight = centerRightWall;
    171.                     centerRight = centerRightWall;
    172.                 }
    173.             }
    174.             #endregion
    175.             #region Top Left and Center Left
    176.             if(northWest == true)
    177.             {
    178.                 if(west == true)
    179.                 {
    180.                     topLeft = centerRoof;
    181.                     centerLeft = centerRoof;
    182.                 }
    183.                 else
    184.                 {
    185.                     topLeft = centerLeftWall;
    186.                     centerLeft = centerLeftWall;
    187.                 }
    188.             }
    189.             else
    190.             {
    191.                 if(west == true)
    192.                 {
    193.                     topLeft = topLeftInCorner;
    194.                     centerLeft = centerRoof;
    195.                 }
    196.                 else
    197.                 {
    198.                     topLeft = centerLeftWall;
    199.                     centerLeft = centerLeftWall;
    200.                 }
    201.             }
    202.             #endregion
    203.         }
    204.         else
    205.         {
    206.             topCenter = topCenterWall;
    207.             #region Top Right and Center Right 2
    208.             if(northEast == true)
    209.             {
    210.                 if(east == true)
    211.                 {
    212.                     topRight = topCenterWall;
    213.                     centerRight = centerRoof;
    214.                 }
    215.                 else
    216.                 {
    217.                     topRight = topRightOutCorner;
    218.                     centerRight = centerRightWall;
    219.                 }
    220.             }
    221.             else
    222.             {
    223.                 if(east == true)
    224.                 {
    225.                     topRight = topCenterWall;
    226.                     centerRight = centerRoof;
    227.                 }
    228.                 else
    229.                 {
    230.                     topRight = topRightOutCorner;
    231.                     centerRight = centerRightWall;
    232.                 }
    233.             }
    234.             #endregion
    235.             #region Top Left and Center Left 2
    236.             if(northWest == true)
    237.             {
    238.                 if(west == true)
    239.                 {
    240.                     topLeft = topCenterWall;
    241.                     centerLeft = centerRoof;
    242.                 }
    243.                 else
    244.                 {
    245.                     topLeft = topLeftOutCorner;
    246.                     centerLeft = centerLeftWall;
    247.                 }
    248.             }
    249.             else
    250.             {
    251.                 if(west == true)
    252.                 {
    253.                     topLeft = topCenterWall;
    254.                     centerLeft = centerRoof;
    255.                 }
    256.                 else
    257.                 {
    258.                     topLeft = topLeftOutCorner;
    259.                     centerLeft = centerLeftWall;
    260.                 }
    261.             }
    262.             #endregion
    263.         }
    264.         // South
    265.         if (south == true)
    266.         {
    267.             bottomCenter = centerRoof;
    268.             #region Bottom Right
    269.             if(southEast == true)
    270.             {
    271.                 if(east == true)
    272.                 {
    273.                     bottomRight = centerRoof;
    274.                 }
    275.                 else
    276.                 {
    277.                     bottomRight = centerRightWall;
    278.                 }
    279.             }
    280.             else
    281.             {
    282.                 if(east == true)
    283.                 {
    284.                     bottomRight = bottomRightInCorner;
    285.                 }
    286.                 else
    287.                 {
    288.                     bottomRight = centerRightWall;  
    289.                 }
    290.             }
    291.             #endregion
    292.             #region Bottom Left
    293.             if(southWest == true)
    294.             {
    295.                 if(west == true)
    296.                 {
    297.                     bottomLeft = centerRoof;
    298.                 }
    299.                 else
    300.                 {
    301.                     bottomLeft = centerLeftWall;
    302.                 }
    303.             }
    304.             else
    305.             {
    306.                 if(west == true)
    307.                 {
    308.                     bottomLeft = bottomLeftInCorner;
    309.                 }
    310.                 else
    311.                 {
    312.                     bottomLeft = centerLeftWall;
    313.                 }
    314.             }
    315.             #endregion
    316.         }
    317.         else
    318.         {
    319.             bottomCenter = bottomCenterWall;
    320.             #region Bottom Right 2
    321.             if(southEast == true)
    322.             {
    323.                 if(east == true)
    324.                 {
    325.                     bottomRight = bottomCenterWall;
    326.                 }
    327.                 else
    328.                 {
    329.                     bottomRight = bottomRightOutCorner;
    330.                 }
    331.             }
    332.             else
    333.             {
    334.                 if(east == true)
    335.                 {
    336.                     bottomRight = bottomCenterWall;
    337.                 }
    338.                 else
    339.                 {
    340.                     bottomRight = bottomRightOutCorner;  
    341.                 }
    342.             }
    343.             #endregion
    344.             #region Bottom Left 2
    345.             if(southWest == true)
    346.             {
    347.                 if(west == true)
    348.                 {
    349.                     bottomLeft = bottomCenterWall;
    350.                 }
    351.                 else
    352.                 {
    353.                     bottomLeft = bottomLeftOutCorner;
    354.                 }
    355.             }
    356.             else
    357.             {
    358.                 if(west == true)
    359.                 {
    360.                     bottomLeft = bottomCenterWall;
    361.                 }
    362.                 else
    363.                 {
    364.                     bottomLeft = bottomLeftOutCorner;
    365.                 }
    366.             }
    367.             #endregion
    368.         }
    369.  
    370.    
    371.  
    372.         sprites[0]  = bottomLeft.texture;
    373.         sprites[1]  = bottomCenter.texture;
    374.         sprites[2]  = bottomRight.texture;
    375.         sprites[3]  = centerLeft.texture;
    376.         sprites[4]  = centerCenter.texture;
    377.         sprites[5]  = centerRight.texture;
    378.         sprites[6]  = topLeft.texture;
    379.         sprites[7]  = topCenter.texture;
    380.         sprites[8]  = topRight.texture;
    381.  
    382.  
    383.  
    384.         int x,y;
    385.         int xx = 96;
    386.         int yy = 96;
    387.  
    388.         for(x = 0; x < xx;x++)
    389.         {
    390.  
    391.             for(y = 0; y < yy; y++)
    392.             {
    393.                
    394.  
    395.             }
    396.  
    397.             //bot
    398.             if(x<32)
    399.             {
    400.                 if(y<32)
    401.                 {
    402.                     Color[] a = sprites[0].GetPixels(0, 0, 32, 32);
    403.                     wallTexture.SetPixels(x ,y ,96,96, a);
    404.                     if(y>32 && y<64)
    405.                     {
    406.                         Color[] b = sprites[3].GetPixels(0, 0, 32, 32);
    407.                         wallTexture.SetPixels(x ,y ,96,96, b);
    408.                     }
    409.                     if(y>64 && y<96)
    410.                     {
    411.                         Color[] c = sprites[6].GetPixels(0, 0, 32, 32);
    412.                         wallTexture.SetPixels(x ,y ,96,96, c);
    413.                     }
    414.                 }
    415.  
    416.             }
    417.             if(x>32 && x<64)
    418.             {
    419.                 if(y<32)
    420.                 {
    421.                     Color[] d = sprites[1].GetPixels(0, 0, 32, 32);
    422.                     wallTexture.SetPixels(x ,y ,96,96, d);
    423.                     if(y>32 && y<64)
    424.                     {
    425.                         Color[]  e = sprites[4].GetPixels(0, 0, 32, 32);
    426.                         wallTexture.SetPixels(x ,y ,96,96, e);
    427.                     }
    428.                     if(y>64 && y<96)
    429.                     {
    430.                         Color[] f = sprites[7].GetPixels(0, 0, 32, 32);
    431.                         wallTexture.SetPixels(x ,y ,96,96, f);
    432.                     }
    433.                 }
    434.             }
    435.             if(x>64 && x<96)
    436.             {
    437.                 if(y<32)
    438.                 {
    439.                     Color[] g = sprites[2].GetPixels(0, 0, 32, 32);
    440.                     wallTexture.SetPixels(x ,y ,96,96, g);
    441.                     if(y>32 && y<64)
    442.                     {
    443.                         Color[]  h = sprites[5].GetPixels(0, 0, 32, 32);
    444.                         wallTexture.SetPixels(x ,y ,96,96, h);
    445.                     }
    446.                     if(y>64 && y<96)
    447.                     {
    448.                         Color[] i = sprites[8].GetPixels(0, 0, 32, 32);
    449.                         wallTexture.SetPixels(x ,y ,96,96, i);
    450.                     }
    451.                 }
    452.             }
    453.  
    454.         }
    455.  
    456.         wallTexture.filterMode = FilterMode.Point;
    457.         wallTexture.wrapMode = TextureWrapMode.Clamp;
    458.         wallTexture.Apply();
    459.    
    460.         wallRenderer.sprite = Sprite.Create(wallTexture, new Rect(0, 0, wallTexture.height, wallTexture.height), new Vector2(0.5f, 0.5f),96f);
    461.  
    462.  
    463.     }
    464.    
    465. }
    466.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to put stuff like "new Texture2D(96,96)" inside a function. You can only declare variables outside functions, not run code.

    --Eric
     
  5. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    Ok I moved it but I still get the error :/
     
  6. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    I think there is something with my for loop because my texture is 96x96 and the sprites are 32x32
     
  7. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    Ok i was able to narrow it down a bit now i get this error

    Array size must be at least width*height
    UnityEngine.Texture2D:SetPixels(Int32, Int32, Int32, Int32, Color[])
    Wall:eek:nCreate() (at Assets/Assets/Code/Rpg/Tile/Wall/Wall.cs:404)
    Wall:Start() (at Assets/Assets/Code/Rpg/Tile/Wall/Wall.cs:80)

    I think it may be because the sprites are 32x32 and the texture is 96x96
    I moved some of my code around
    Code (CSharp):
    1. for(x = 0; x < xx;x++)
    2.         {
    3.  
    4.             for(y = 0; y < yy; y++)
    5.             {
    6.                 if(x<32)
    7.                 {
    8.                     if(y<32)
    9.                     {
    10.                         Color[] a = sprites[0].GetPixels(0, 0, 32, 32);
    11.                         wallTexture.SetPixels(x ,y ,96,96, a);
    12.                     }
    13.                     if(y>32 && y<64)
    14.                     {
    15.                         Color[] b = sprites[3].GetPixels(0, 0, 32, 32);
    16.                         wallTexture.SetPixels(x ,y ,96,96, b);
    17.                     }
    18.                     if(y>64 && y<96)
    19.                     {
    20.                         Color[] c = sprites[6].GetPixels(0, 0, 32, 32);
    21.                         wallTexture.SetPixels(x ,y ,96,96, c);
    22.                     }
    23.                    
    24.                 }
    25.                 if(x>32 && x<64)
    26.                 {
    27.                     if(y<32)
    28.                     {
    29.                         Color[] d = sprites[1].GetPixels(0, 0, 32, 32);
    30.                         wallTexture.SetPixels(x ,y ,96,96, d);
    31.                     }
    32.                     if(y>32 && y<64)
    33.                     {
    34.                         Color[]  e = sprites[4].GetPixels(0, 0, 32, 32);
    35.                         wallTexture.SetPixels(x ,y ,96,96, e);
    36.                     }
    37.                     if(y>64 && y<96)
    38.                     {
    39.                         Color[] f = sprites[7].GetPixels(0, 0, 32, 32);
    40.                         wallTexture.SetPixels(x ,y ,96,96, f);
    41.                     }
    42.                 }
    43.                 if(x>64 && x<96)
    44.                 {
    45.                     if(y<32)
    46.                     {
    47.                         Color[] g = sprites[2].GetPixels(0, 0, 32, 32);
    48.                         wallTexture.SetPixels(x ,y ,96,96, g);
    49.                     }
    50.                     if(y>32 && y<64)
    51.                     {
    52.                         Color[]  h = sprites[5].GetPixels(0, 0, 32, 32);
    53.                         wallTexture.SetPixels(x ,y ,96,96, h);
    54.                     }
    55.                     if(y>64 && y<96)
    56.                     {
    57.                         Color[] i = sprites[8].GetPixels(0, 0, 32, 32);
    58.                         wallTexture.SetPixels(x ,y ,96,96, i);
    59.                     }
    60.                 }
    61.  
    62.             }
    63.  
     
  8. McSpiffy

    McSpiffy

    Joined:
    Aug 14, 2013
    Posts:
    11
    Never mind I fixed it