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 /
avatar image
0
Question by fluxhackspro · May 11, 2017 at 02:17 PM · arrayarraysloopforcearraylist

Help with for loop and arrays

Hello so i have a script that is suppose to spawn a card at a random cardSpot. So i tried to get all objects from an array in this case the cardSpots. And then check if the cardspots bool 'isFree' is true. Then i spawn a random card from an other array at that chosen cardspots position. But i get an error that says 'Index out of range' i checked the script and everything seems to be fine. Would be glad if someone could tell me what the issue is and why. Im sorry if it's really easy im new to loops and arrays! Anyhow thanks!

~flux

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Deck : MonoBehaviour {
 
     public GameObject[] cards;
     public GameObject[] cardSpots;
 
     public void GiveCard ()
     {
         Debug.Log ("function worked");
 
         for (int i = 0; i < cardSpots.Length; i++)
         {
             if (cardSpots [i].GetComponent<Spot> ().isFree == true)
             {
                 Instantiate(cardSpots[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
                 cardSpots [i].GetComponent<Spot> ().isFree = false;
                 return;
             }
         }
     }
 }
 
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

3 Replies

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

Answer by HarshadK · May 11, 2017 at 02:22 PM

In your Instantiate you are trying to get an item from cardSpots by generating a random number which is between 0 and length of cards array. So since your cards array has length larger than cardSpots you are getting that error. Your Instantiate statement should be:

 Instantiate(cardSpots[Random.Range(0, cardSpots.Length)], cardSpots[i].transform.position, Quaternion.identity);


Comment
Add comment · Show 2 · 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 NoseKills · May 11, 2017 at 02:47 PM 1
Share

Purely judging by the variable names, I think he wants to instantiate a card from cards-array into a cardspot, not the other way around so

 Instantiate(cards[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
 
avatar image fluxhackspro · May 11, 2017 at 07:12 PM 0
Share

Thank you i understand now (:

avatar image
2

Answer by phxvyper · May 11, 2017 at 03:36 PM

In the line

 Instantiate(cardSpots[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);

You are basing the index you're getting in Random.Range(0, card.Length) off of the cards array, not the cardSpots array. This wouldn't be problematic if both arrays had the same number of items in them, but clearly they have a different number of items.

Is your intention to get a GameObject from cards or from cardSpots? Make sure you're only basing your index off of the right array's length, not a different one.

For example:

 Instantiate(cardSpots[Random.Range(0, cardSpots.Length)], cardSpots[i].transform.position, Quaternion.identity);

or:

 Instantiate(cards[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);

A really good way to stop confusion with variables is to more properly describe what they're doing. For example, if you're variable is intended to be a collection of Prefabs - not just game objects - call it CardPrefabs instead of just Cards, et cetera.

Comment
Add comment · Show 1 · 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 carlqwe · May 11, 2017 at 07:11 PM 0
Share

I had two errors and this was one of them, a typo thank you for your answer!

avatar image
-3

Answer by leech54 · May 11, 2017 at 03:20 PM

Random.Range(0, cards.Length) does not include the 0 but does include the cards.Length I think that makes it go out of bounds.

You can do

 int i = Random.Range(0,cards.Length);
 
 Instantiate(cards[i-1]),...)
 

from the Unity manual Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.

Comment
Add comment · Show 2 · 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 Hellium · May 11, 2017 at 03:21 PM 0
Share

Read carefully the documentation :

public static int Range(int $$anonymous$$, int max); Description

Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).

Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals $$anonymous$$, $$anonymous$$ will be returned.

avatar image leech54 Hellium · May 11, 2017 at 03:37 PM 0
Share

I guess it matters if you use floats or int. It is done different.

public static float Range(float $$anonymous$$, float max);

Description

Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive] (Read Only).

Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.

vs

public static int Range(int $$anonymous$$, int max);

Description

Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).

Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals $$anonymous$$, $$anonymous$$ will be returned.

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

8 People are following this question.

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

Related Questions

Unity C# Highscore table 2 Answers

Linear Interpolation of Array Values 1 Answer

Get all GameObjects by variable value 2 Answers

using arrays to perform multiple tasks 1 Answer

Problems with Arrays 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