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 chesart · May 30, 2012 at 04:59 PM · arrayrandomnumbergenerator

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

Comment
Add comment · Show 2
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 flamy · May 30, 2012 at 06:01 PM 0
Share

have you attached the textures in the editor? if not this wont work

avatar image chesart · May 30, 2012 at 09:14 PM 0
Share

Yes, textures are attached. When I commented string : is+=1; error message is gone , but I receive only one texture of four.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 25 · 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 chesart · May 30, 2012 at 08:59 PM 0
Share

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.

avatar image whydoidoit · May 30, 2012 at 09:02 PM 0
Share

So what is "is"?

avatar image chesart · May 30, 2012 at 09:19 PM 0
Share

Without 'is' textures wouldn't change.

avatar image whydoidoit · May 30, 2012 at 10:09 PM 0
Share

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.

avatar image chesart · May 30, 2012 at 10:10 PM 0
Share

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.

Show more comments
avatar image
0

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;

Comment
Add comment · Show 3 · 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 whydoidoit · Jun 01, 2012 at 01:44 PM 0
Share

He is not setting is = to a random range, he is setting a = random range

avatar image chesart · Jun 01, 2012 at 06:38 PM 0
Share

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.

avatar image chesart · Jun 03, 2012 at 01:49 PM 0
Share

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer

Generate 8 unique random integers 2 Answers

random number generator problem 1 Answer

Random Number Generator 4 Answers

How to prevent picking the same combination in array? 1 Answer


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