- Home /
Random texture changer (problem with array)
Hi there, i have a problem with an array and I don't understand why I have this error. Here is a code
private var message = 0;
var maljbert1 : GameObject;
var maljbert2 : GameObject;
private var x : int;
var a : int;
var texture : Texture[];
private var is = 0;
function Start () {
a = Mathf.RoundToInt(Random.Range (1,4)) ; Debug.Log (a);
}
function Update () {
if (a>1){
maljbert1.renderer.material.mainTexture = texture[is]; is += 1; }
}
And every time when I hit play button I recieve message : IndexOutOfRangeException: Array index is out of range. I have GameObject with four textures and this script attatched to it . I hope you can help me. Cheslav
have you attached the textures in the editor? if not this wont work
Yes, textures are attached. When I commented string : is+=1; error message is gone , but I receive only one texture of four.
Answer by whydoidoit · May 30, 2012 at 06:05 PM
You are looking up the texture using a variable called is which increments with no limit that I can see, that will quickly go out of range.
You specify a in start, if it is greater than one then you will just keep incrementing* is* forever - this will cause the index to be out of range.
I don't think you have quite thought through that logic of what your "a" and "is" variables are doing - I'm guessing you really want to be setting a texture based off the value of "a" but can't be sure
So, variable 'a' is for number randomiser, from this int I get a random number from 1 to 4 and multiply all this by 10. Here is a string of a code : a = $$anonymous$$athf.RoundToInt(Random.Range (1,4)*10); Sorry I forgot to write *10 in code above. I want to use var 'a' to use generated value from it to insert it into if (){} and according to this value, change a texture of fisrt or second gameObject. Actually I don't really understand ,how to fix this issue.
Yes I can see that from the code - but was is it, why are you adding one to it every frame when you only have 4 textures? I don't understand what it represents and where it's value may be clamped.
According to the code you have posted is starts at 0 and then goes 1,2,3,4,5,6,7,8,9,10,11... So long as the value of a is greater than 1. As you have only 4 textures your code will break after the fourth frame, approximately 1/15th of a second after you start the game.
Do You know , how I can realise texture/material changing but without is+=1? And one more question , I need to make a touch event script for two game objects e.g. cloth and all this must be connected to this script . So , if you press cloth with index = 0 , you will get it opened in the right place , where texture of the gameObject was changed and cloth with index = 1 will not change a texture of the second gameObject . But one moment , these clothes must change their position randomly too, so they must have fixed position on the screen . P.S. sorry for my english.
Answer by arvind53 · Jun 01, 2012 at 01:44 PM
you made...private var is=0; but you declared range as..Random.Range(1,4), make it.. is=Random.Range(0,4), and assign five diff. textures to array.. manually or automatically.. texture[0]=Texture0; texture[1]=Texture1; texture[2]=Texture2; texture[3]=Texture3; texture[4]=Texture4;
He is not setting is = to a random range, he is setting a = random range
So, Arvindsudarshan53 , I will have more than four pictures , I'll use at least 60 pictures , I think it will be a hard work to wtire this code for 60 pictures, but if pictures value will be 120 or greater ? I think , it is better to randomly change textures using Random.Range (1,n+1) ins$$anonymous$$d of your solution, but anyway thank you for your advice, I think I can use this in my next projects.
Hello $$anonymous$$ike, I have one more problem , can you help me to solve it ? So , I have these two curtains , but I need touch events on them , I have two scripts : Scores and $$anonymous$$inusLife , there is touch code connected to ray cast hit but nothing is working. Also I have touch test script , it shows me where touch phase begins and ends and also finger counts. I really don't understand , why other scripts can't work. If I use simple touch script to instantiate some game object, it works , but when I add scoreText to string it does nothing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072138im_/https://answers.unity.com/themes/thub/images/avi.jpg)