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 Glaydur · Sep 28, 2014 at 10:00 PM · lerpindexoutofrangeexceptioncolor.lerp

IndexOutOfRange when using Lerp

So here is the relevant code:

 void Update () {
     //t += Time.deltaTime;
     //print(t);
 
     renderer.material.color = Color.Lerp(start, end, 5);
 
     if (renderer.material.color.a <= 0.0)
         Destroy(gameObject);
         
 }

So I don't get why it's giving the IndexOutOfRange exception. I'm most certainly sure that it's the number I give it(5 in this case). I have even tried putting an f after the numner(like 5f) to make sure it's a float but to no avail. Any clarification would be really helpful. Maybe I'm just not understanding Lerp enough.

Edit: So I noticed that this isn't all the relevant code. The exception is actually happening in a whole different class which is this one:

 public class RandomSprite : MonoBehaviour {
 
     public Sprite[] sprites;
     public string resourceName;
     public int currentSprite = -1;
 
     // Use this for initialization
     void Start () {
         if (resourceName != "") {
             sprites = Resources.LoadAll<Sprite>(resourceName);
             if (currentSprite == -1)
                 currentSprite = Random.Range(0, sprites.Length);
             else if (currentSprite > sprites.Length)
                 currentSprite = sprites.Length - 1;
             **GetComponent<SpriteRenderer>().sprite = sprites[currentSprite];**
         }
     
     }



The line within the asterisks .. is the line that the error is pointing to.

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
Best Answer

Answer by robertbu · Sep 28, 2014 at 10:06 PM

There is nothing here that will generate an IndexOutOfRange. You need to provide your entire script and a copy of the exact error message from the console. As for this code, Lerp() takes values between 0.0 and 1.0. A value of 0.5 will produce an output half way between 'start' and 'end'. A value of 5 will get rounded to 1 and will return 'end'. There is an eased form of Lerp that can be used this way:

  renderer.material.color = Color.Lerp(renderer.material.color, end, Time.deltaTime * 5.0);
Comment
Add comment · Show 4 · 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 Glaydur · Sep 28, 2014 at 10:10 PM 0
Share

Thanks for the correction. I have edited the original post.

avatar image robertbu · Sep 28, 2014 at 11:14 PM 0
Share

That line will generate an error if the sprites array does not have any entries. That is, both lines 12 and 14 will set 'currentSprite' to 0, which is not valid for an empty array. If you see the array has entries in the inspector, look for a second copy of the script attached somewhere in your hierarchy.

avatar image Glaydur · Sep 30, 2014 at 10:37 AM 0
Share

Aren't arrays zero based? Why would currentSprite not work if it is set to zero? It will just be pointing to the first element of the array right?

avatar image dmg0600 · Sep 30, 2014 at 11:00 AM 0
Share

If the array does not have any elements at all, there would be no first element to access and would give the IndexOutOfRange error, check it like @robertbu has pointed out to see if this is the case.

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

smoothly switch between 2 colors that are using lerp 2 Answers

Color.Lerp everytime Onclick 1 Answer

If statement not being fullfilled until GameObject inspected in inspector. 0 Answers

Color.Lerp doesn't work 1 Answer

Lerping 2 values on a shader on button press. 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