Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 04, 2020 at 05:35 PM by Dragondean for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dragondean · Jan 04, 2020 at 04:33 AM · scripting problemerrorarraycanvaserror message

Array Out of Index (When it Really is not??)

I am making a space game and I thought it would be cool to have a bunch of squares make up a bar that makes up your velocity bar (a bar that shows your velocity lerped between 0 and max velocity). But it has been quite a terrible experience so far.


I have my code, right? I don't change anything in the inspector including anything in the arrays except for assigning stuff to them, no length changes or anything. I know that arrays start at 0 and have it be from 12 however I check that the subtractor (or whatever) is always not equal to 0. SO... THERE IS ABSOLUTELY NO REASON FOR IT TO BE OUT OF RANGE.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class VelocityBar : MonoBehaviour
 {
     public Image[] forwardVelBar = new Image[12];
     public Image[] reverseVelBar = new Image[3];
     [SerializeField] int barsToIlluminate;
     public int barsNotToIlluminate;
     float reverseBars;
     GameObject Player;
     float playerVel;
 
     // Start is called before the first frame update
     void Start()
     {
         Player = GameObject.FindGameObjectWithTag("Player");
         Debug.Log("is this running?");
     }
 
     // Update is called once per frame
     void Update()
     {
         playerVel = Player.GetComponent<SpaceShip>().forwardVel;
 
         barsToIlluminate = (int)((playerVel * 0.3f)*100);
 
         for (int i = 0; i < barsToIlluminate; i++)
         {
             forwardVelBar[i].color = new Color(forwardVelBar[i].color.r, forwardVelBar[i].color.b, forwardVelBar[i].color.g, 255);
         }
 
         barsNotToIlluminate = 12 - barsToIlluminate;
         if(barsNotToIlluminate >= 1)
         {
             for (int i = 0; i < barsNotToIlluminate; i++)
             {
                 forwardVelBar[12-i].color = 
                 new Color(forwardVelBar[12-i].color.r, 
                 forwardVelBar[12-i].color.b, forwardVelBar[12-i].color.g, 85);
             }
         }
     }
 }
 

So I run it and it looks good, at first. The frame after it starts it says array is out of index. However, it literally did its job correctly. Like, bruh just do that again. BUT NO. Attached is a screenshot of the error and the success of a dramatic first frame (Also the script in the inspector).alt text

So my starting velocity is 0.2 and the max is 0.4 so it looks good, six illuminated, six not. So I have been staring at Visual Studio for 10 hours about to eat my own eyes.


All I need in life right now is to figure out how to not have a false (I think) out of index error

screenshot-29.png (119.1 kB)
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

  • Sort: 
avatar image
3
Best Answer

Answer by logicandchaos · Jan 04, 2020 at 04:37 AM

if i=0 then 12-0 = 12 indexing forwardVelBar[12] which is out of bounds. You could start i at 1 or make it 11-i instead of 12-i.

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 Dragondean · Jan 04, 2020 at 04:53 AM 0
Share

No errors, yay! However, for some reason now only the sixth one from the top (A$$anonymous$$A the last one in the image) is not illu$$anonymous$$ated. So only one is illu$$anonymous$$ated which is sort of wack.

avatar image pako Dragondean · Jan 04, 2020 at 04:04 PM 1
Share

@Dragondean your original question has been answered. Post a new question if you want help with another issue.

avatar image lgarczyn Dragondean · Jan 04, 2020 at 06:58 PM 0
Share

This is probably caused by your 6th square not being inside the array.

avatar image Bunny83 Dragondean · Jan 04, 2020 at 08:29 PM 0
Share

Your overall approach is bad. First of all this line:

 barsToIllu$$anonymous$$ate = (int)((playerVel * 0.3f)*100);

could easily create an index out of bounds. At no point do you ensure it stays within 0 - 11. Since you essentially multiply your velocity by 30 your max velocity would be 0.36666666. As soon as your velocity gets larger than 0.4 this expression would return 12 which is out of bounds. I don't really see the point of the "0.3" or the "100".


Since you just want to "illu$$anonymous$$ate" bars depending on your velocity it's much saver and simply to just use one loop that always iterates over all your images and just figure out which should be on / off in the loop

 barsToIllu$$anonymous$$ate = (int)((playerVel * 0.3f)*100);
 
 for (int i = 0; i < forwardVelBar.Length; i++)
 {
     Color col = forwardVelBar[i].color;
     col.a = 0.333333f;
     if(i > barsToIllu$$anonymous$$ate)
         col.a = 1f;
     forwardVelBar[i].color = col;
 }

Also note that you use a Color value. The components of a Color is between 0 and 1, not between 0 and 255. That would be a Color32.

Follow this Question

Answers Answers and Comments

251 People are following this question.

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

Related Questions

Cant fetch components from GameObjects stored in an array? 0 Answers

Help with editing array from inside functions for PID controller (C#)[Fixed] 1 Answer

How do I fix this problem-it won't let me start my game in Unity because of an override problem 1 Answer

Script Error 1 Answer

IndexOutOfRangeException: 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