Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DrakeDiablo · Sep 03, 2014 at 01:10 AM · errortexturebuttonarrayindex

Adding a texture to array textures?!

Hey guys! Im trying to make a thing where in one script I make an array for button(skill bar), so if I enter 3 there will be 3 buttons with their own textures, and in another script I want to add a button and then assign a texture to that new button. It keeps telling me Array index out of range! What should I do?

Skill bar Script: #pragma strict

 public var count : int;
 
 public var SkillsNum : boolean[] = [];
 public var SkillsPos : Rect[];
 public var SkillsNames : String[];
 public var SkillsTexture : Texture2D[];
 
 function Start () {
 
 }
 
 function Update () {
 SkillsPos = new Rect[count];
 SkillsNum = new boolean[count];
 SkillsNames = new String[count];
 SkillsTexture = new Texture2D[count];
 
 }
 
 function OnGUI () {
     for(var i : int = 0; i < SkillsNum.length; i++){
         SkillsNum[i] = GUI.Button(Rect(Screen.width - (Screen.width * 0.6) + 52 * i, Screen.height - (Screen.width * 0.05), 50,50),SkillsTexture[i]);
         if(SkillsNum[i]){
             
         }
     }
 }

other script(part of it, rest is the EquipmentEffect from Brakeys):

 var texture : Texture2D;
 
 Player.GetComponentInChildren(SkillBar_02).count += 1;
         Player.GetComponentInChildren(SkillBar_02).SkillsTexture[i] = texture;


"count" determines how many buttons there are.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zharik86 · Sep 03, 2014 at 08:02 AM

On how many I see, you change the array size, but you don't change an array. And it isn't clear that for the variable "i" in the second piece of a code. I will a little change your code and I will add comments. First script by name "SkillBars_02":

  #pragma strict

  public var count: int;
 
  public var SkillsNum: boolean[];
  public var SkillsPos: Rect[];
  public var SkillsNames: String[];
  public var SkillsTexture: Texture2D[];
 
  function Start () {
   //Here initialization your arrays. It isn't necessary to create each frame them as Update ()
   SkillsPos = new Rect[count];
   SkillsNum = new boolean[count];
   SkillsNames = new String[count];
   SkillsTexture = new Texture2D[count];
   //After you fill arrays with their values
  }
 
  function Update () {

  }
 
  function OnGUI () {
   for(var i: int = 0; i < SkillsNum.Length; i++) {
    SkillsNum[i] = GUI.Button(Rect(Screen.width - (Screen.width * 0.6) + 52 * i, Screen.height - (Screen.width * 0.05), 50,50),SkillsTexture[i]);
    if(SkillsNum[i]) {
 
    }
   }
  }

  //Create new function, which addind new texture to array
  function myAddTexture(tpTex: Texture2D, tpRect: Rect, tpName: String) {
   Debug.Log("Value count = " + count + " value Length = " + SkillsTexture.Length);
   //Values "count" and "Length" shall be equal

   count++; //increase value of count elements of arrays
   //create temp array for new textures
   var tpSkillsTexture: Texture2D[] = new Texture2D[count];
   //create temp array for other elemets skills
   var tpSkillsPos: Rect[] = new Rect[count];
   var tpSkillsNum: boolean[] = new boolean[count];
   var tpSkillsNames: String[] = new String[count];
   //Fill arrays previos data and last index new data
   for(vat i: int = 0; i < count - 1; i++) {
    Debug.Log("Store element i = " + i);
    tpSkillsTexture[i] = SkillsTexture[i];
    tpSkillsPos[i] = SkillsPos[i];
    tpSkillsNum[i] = SkilsNum[i];
    tpSkillsNames[i] = SkillsNames[i];
   }
   //And add new skill
   tpSkillsTexture[count - 1] = tpTex;
   tpSkillsPos[count - 1] = tpRect;
   tpSkillsNum[count - 1] = false;
   tpSkillsNames[count -1] = tpName;
   //Now we remember new reference
   SkillsTexture = tpSkillsTexture;
   SkillsPos = tpSkillsPos;
   SkillsNum = tpSkillsNum;
   SkillsNames = tpSkillsNames;
  }

And second script(you in it shall have texture, a rect and a name of a new skill), more precisely only its part.

  Player.GetComponentInChildren(SkillBar_02).myAddTexture(texture, Rect(50, 100, 50, 50), "YourSkillName");

In the second part it isn't necessary to change count value. It changes as myAddTexture(). Rect and Name you can set any which you think the correct. I hope that it will help you.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DrakeDiablo · Sep 03, 2014 at 01:02 PM 0
Share

Hey! thanks for replying! :D Damn, I seem to have a load of errors lol. Anyways, you had some typos in the variable names(you wrote Skill, ins$$anonymous$$d of Skills, so u just forgot "s" for every variable) I fixed that, and now every time I try to equip something so it adds me a new button, it says Array index out of range, and it points to the function myAddTexture. Inside that function tpSkillsTexture[i] = SkillsTexture[i], that is the line where the error leads to, and are u sure that it suppose to be tpSkillsTexture[count - 1] = tpTex ? i think it should be a + ins$$anonymous$$d of -, i not sure, im not a good programmer, so can you please help again? :D

avatar image zharik86 · Sep 03, 2014 at 08:11 PM 0
Share

@DrakeDiablo Thanks for an error with syntax. I corrected the answer. Now I will write you the basics work of arrays and their indexes(write on JavaScript):

  //For example, we need to create int array on 14 elements
  var temp: int[] = new int[14];
  //Count elements in our array is 14, but index element from 0 to 13.
  var b: int = temp[0]; //variable "b" is equal first element of array
  var c: int = temp[13]; //variable "c" is equal last element of array
  var d: int = temp[14]; //Error: Array index out of range
  var e: int = temp.Length; //in our case variable "e" is equal 14

And now about an error. $$anonymous$$ost likely you somewhere else change "count" value. Check that it changed only as myAddTexture(). And for clearing up of errors you can use Debug.Log (). It will allow you to see messages in the console. I will a little add the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

array problem 1 Answer

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

C# array not behaving as expected 0 Answers

The index value is not starting from 0 0 Answers

IndexOutOfRangeExeption - Array index is out of range 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges