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 /
  • Help Room /
avatar image
0
Question by Bondolero · Feb 14, 2016 at 11:06 PM · spawnspawningcoinscoin

Array index is out of range.

Hello i have almost finished my 2D mobile game but i have a very last issue stepping in my way that i can't figure out the solution so i'm hoping that someone could help me. Here is the code:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class collectDimonds : MonoBehaviour {

 public static int dimonds = 0;
 public int dimondValue = 1;

 public Transform[] dimondSpawn;
 public Image dimond;
 
 public bool spawning = false;


 // Use this for initialization
 void Start () {
     dimonds = PlayerPrefs.GetInt ("Dimanti");

 }
 
 void OnTriggerEnter2D(Collider2D coll)
 {
     if (coll.gameObject.tag == "Dimond") 
     {
     
         dimonds += dimondValue;
         Destroy(coll.gameObject);
         spawning = false;



         
     }
 }
 void Update()
 {
     PlayerPrefs.SetInt("Dimanti", dimonds);
     if (!spawning) {

         Spawn();
     }

 }

 void Spawn () 
 {
     
     spawning = true;
     
     
     Transform coinSpawns = dimondSpawn [Random.Range (0, dimondSpawn.Length)];
     Image coinPrefab = (Image)Instantiate (dimond, coinSpawns.position, coinSpawns.rotation);
     coinPrefab.transform.SetParent(GameObject.FindObjectOfType<Canvas>().gameObject.transform);
     
 }


}

And the error says:

IndexOutOfRangeException: Array index is out of range. collectDimonds.Spawn() (at Assets/scripts/collectDimonds.cs :54)

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 gjf · Feb 14, 2016 at 01:57 AM 0
Share

probably because dimondSpawn.Length is one too many.

you should move the PlayerPrefs.SetInt() to the trigger code - there's no need to set it every frame if it hasn't changed.

however, it's difficult to say whether that's even needed - are you using PlayerPrefs to share values between scripts? it's generally used to store value between game sessions...

avatar image Bondolero gjf · Feb 14, 2016 at 06:23 PM 0
Share

So i moved PlayerPrefs.SetInt() to the trigger code, but i still can't figure out the error

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by EmHuynh · Feb 15, 2016 at 04:47 AM

Hello, @Bondolero!

The problem is in this line: Transform coinSpawns = dimondSpawn [Random.Range (0, dimondSpawn.Length)];

The max range of Random.Range should be the length of array dimondSpawn subtracted by 1. Because you want the the last index of dimondSpawn.

So this should work: Transform coinSpawns = dimondSpawn [Random.Range (0, dimondSpawn.Length - 1)];

Here's a sample:

 int[] intArray = new int[ 3 ] { 2, 4, 6 };
 
     void Update()
     {
         // Invalid. Error: IndexOutOfRangeException:
         // Array is out of range.
         Debug.Log( intArray[ intArray.Length ] );
 
         // Valid. Length of array subtracted by 1.
         Debug.Log( intArray[ intArray.Length - 1 ] );
     }

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[C#] Only one instantiate object is working, the rest is not.. Help meee 0 Answers

How do you spawn a gameobject based on last object in array and limit that within a range? 1 Answer

How to make a coin bank? 0 Answers

uNet Spawning Objects 0 Answers

Coin Bank Issues in [Code C#] 0 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