1. Unity 5.6 is now released.
    Dismiss Notice
  2. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  3. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. The launcher is currently pulling earlier versions of the content packages which may have errors, see post.
    Dismiss Notice
  6. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  7. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice

Texture2d array

Discussion in 'Scripting' started by jessee03, Mar 5, 2013.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    652
    Can't seem to get my Texture2d array to work correctly. Maybe someone might know what's wrong. It's going to be for keeping track of equipped items. Maybe there's a better way of doing this. I keep getting indexOutOfRange exception.

    Code (csharp):
    1.  
    2.  
    3. //main code
    4.  
    5. //inventory icons
    6. var weaponSlot : Texture2D[] = new Texture2D[11];
    7.  
    8.  
    9. function OnGUI () {
    10.  
    11.     //top item row
    12.     if (GUI.Button(Rect((Screen.width/2) - 796,(Screen.height/2) - 297,50,50),weaponSlot[0])) {
    13.  
    14.         }
    15.         if (GUI.Button(Rect((Screen.width/2) - 717,(Screen.height/2) - 297,50,50),weaponSlot[1])) {
    16.  
    17.         }
    18.         if (GUI.Button(Rect((Screen.width/2) - 632,(Screen.height/2) - 297,50,50),weaponSlot[2])) {
    19.    
    20.         }
    21.         if (GUI.Button(Rect((Screen.width/2) - 547,(Screen.height/2) - 297,50,50),weaponSlot[3])) {
    22.  
    23.         }
    24.         if (GUI.Button(Rect((Screen.width/2) - 457,(Screen.height/2) - 297,50,50),weaponSlot[4])) {
    25.      
    26.         }
    27.        
    28.        
    29.         //bottom item row
    30.     if (GUI.Button(Rect((Screen.width/2) - 796,(Screen.height/2) - 207,50,50),weaponSlot[5])) {
    31.  
    32.         }
    33.         if (GUI.Button(Rect((Screen.width/2) - 717,(Screen.height/2) - 207,50,50),weaponSlot[6])) {
    34.        
    35.         }
    36.         if (GUI.Button(Rect((Screen.width/2) - 632,(Screen.height/2) - 207,50,50),weaponSlot[7])) {
    37.      
    38.         }
    39.         if (GUI.Button(Rect((Screen.width/2) - 547,(Screen.height/2) - 207,50,50),weaponSlot[8])) {
    40.      
    41.         }
    42.         if (GUI.Button(Rect((Screen.width/2) - 457,(Screen.height/2) - 207,50,50),weaponSlot[9])) {
    43.        
    44.         }
    45.  
    46. }
    47.  
    48.  
    49.  
    50.  
    51.  
    52. //script attached to each item prefab
    53.  
    54.  
    55. var item_attackPower : float;
    56. var item_type : int;
    57. var item_icon : Texture2D;
    58.  
    59. //item_type. 0 = weapon, 1 = helm, 2 = shoulders
    60.  
    61.  
    62.  
    63. function Start () {
    64.  
    65.  
    66. playerStats.attackPower  += item_attackPower;
    67.  
    68. itemType();
    69.  
    70. }
    71.  
    72.  
    73.  
    74.  
    75. function Update () {
    76. }
    77.  
    78.  
    79.  
    80. function itemType () {
    81.  
    82. var otherScript: playerStats = gameObject.Find("player").GetComponent(playerStats);
    83.  
    84. switch (item_type){
    85. case ("0"):
    86. otherScript.weaponSlot[0] = item_icon;
    87. break;
    88. case ("1"):
    89. otherScript.weaponSlot[1] = item_icon;
    90. break;
    91. case ("2"):
    92. otherScript.weaponSlot[2] = item_icon;
    93. break;
    94.  
    95.  
    96. }
    97.  
    98. }
    99.  
    100.  
    101.  
    102.  
    103.  
    104.  
    105.  
    106.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,001
    Public variables get their values from the inspector. So don't do this sort of thing:

    Code (csharp):
    1. var weaponSlot : Texture2D[] = new Texture2D[11];
    Since that implies there should be 11 items in the array, but it's not defined by the code, it's defined by what you put in the inspector. Just do this:

    Code (csharp):
    1. var weaponSlot : Texture2D[];
    If you want to have 11 items as the initial default value, put that in the Reset function:

    Code (csharp):
    1. function Reset () {
    2.     weaponSlot = new Texture2D[11];
    3. }
    That way it will be set to 11 entries in the array by default and when using the Reset menu item. However the actual value is always controlled by the inspector. If you get an out of range error, that means you're referring to items that are not in the array. So if you say weaponSlot[10] and get the error, that means you didn't put at least 11 items in the array.

    --Eric
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    652
    I just came back to post that that's what I just figured out haha. I'm having an issue with my switch statement though for some reason. No matter what number I have my item_type set to nothing is being called. I did a Debug and the function is still being called at the Start.