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 26, 2019 at 08:11 AM by $$anonymous$$ for the following reason:

Too subjective and argumentative

avatar image
1
Question by $$anonymous$$ · Aug 17, 2018 at 10:41 AM · arrayshootingscriptableobjectscript errorindexoutofrangeexception

Problem with arrays and accessing sriptable objects.

 public Weapon[] weapons;
 public int currentWeapon;

     shootRay.origin = weapons[currentWeapon].bulletSpawnPoint.transform.position;
     shootRay.direction = weapons[currentWeapon].bulletSpawnPoint.transform.forward;

     if (Physics.Raycast(shootRay, out raycastHit, weapons[currentWeapon].range))

    .......

I want to access variables in Weapon script (scriptableobject) from Shooting(up) script but i have error: IndexOutRangeException: Array index is out of range.

Please help and thx.

Comment
Add comment · Show 3
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 eses · Aug 17, 2018 at 12:57 PM 1
Share

Hi @berkgokhann - Have you actually assigned anything to your array in inspector / from code or are you accessing your array past it's last element perhaps?

avatar image RonTang · Aug 17, 2018 at 01:35 PM 0
Share

You should check the weapons length and currentWeapon. $$anonymous$$ake sure you actually assigned some scriptableobject in the inspector.

avatar image $$anonymous$$ · Aug 17, 2018 at 06:27 PM 0
Share

@eses and @RonTang Firstly i thanks for answers. I create 3 weapon (scriptableobject) and assing them to array from inspector.

2 Replies

  • Sort: 
avatar image
0

Answer by madks13 · Aug 17, 2018 at 08:40 PM

  1. Always check for array lengths. Unless it's static arrays.

  2. You should use polymorphism to shoot a weapon. Rather than having a single external class managing how each weapon shoots, implement a method called Shoot or something explicit like that and call it for the current weapon. That way your external class won't have to worry about how shooting is done, it will simply know it can shoot.

Comment
Add comment · 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
0

Answer by RustyCrow · Aug 17, 2018 at 10:15 PM

Form what i can see there is 3 things that come to mind.

  1. Your array is not filled the way you think it is, so when you say currentWeapon= 6 the array might only have 4(OR 0) actual weapons in the array . Double check the inspector and count how meny you have / see.

  2. Some other scripts is messing with your values, are you 100% that currentWeapon and weapons or only being used in this scripts. They are public so there is a chance maybe you or someone els did something somewhere.

  3. Remember arrays/indexes start at 0 so if currentWeapon = 0 that is the first(1) weapon, so if currentWeapon = 1 that is your second(2) weapon. Computers AM i rite? LEL

Might not help right now but this is a very helpful tool when debugging just print some values out see what is happening with your currentWeapon and Weapon.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

109 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

Related Questions

IndexOutOfRangeException: Array index is out of range. 2 Answers

Set types of scripts i can place in ScriptableObject[] 1 Answer

Problem with Singleton and NullReferenceException? 0 Answers

How to learn amount of objects in selected prefabs folder? 0 Answers

IndexOutOfRangeException error for array points in range 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