• 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 TurboHermit · Aug 02, 2012 at 06:31 PM · arrayrandomrandom.rangerange

How to know what random number is chosen

I'm using Random.Range to pick a random card from an array, I'd like to delete it from the array afterward, but how do I know which index number he picked at random?

var CurCard = Instantiate(P1Deck[Random.Range(0, P1Deck.Length)], CardLocations[LocationNumber].transform.position, CardLocations[LocationNumber].transform.rotation);

Comment
Add comment · Show 6
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 TurboHermit · Aug 02, 2012 at 06:50 PM 0
Share

Well I've determined what the random is by declaring a variable like this:

var RandomPick = Random.Range(0, P1Deck.Length);

var CurCard = Instantiate(P1Deck[RandomPick], CardLocations[LocationNumber].transform.position, CardLocations[LocationNumber].transform.rotation);

But now the question is how to remove it. I've tried RemoveAt,

like this: P1Deck.RemoveAt(RandomPick),

but I'm getting this error:

'RemoveAt' is not a member of 'UnityEngine.GameObject[]'.

avatar image Screenhog · Aug 02, 2012 at 07:10 PM 0
Share

GameObject[] is a built-in array. One of its properties is that you've assigned it a size, and throughout the lifetime of your game, its size won't change.

If you need to have an array that has a changing length, you'll need to use some other kind of array, like a List. However, I suppose you could also just make that element of the array null:

P1Deck[RandomPick] = null;

But this may cause other issues with your code (such as trying later to pick a card out of the deck that doesn't exist). You'll need to make sure that null cards in your code are ignored later. I'd probably lean to making a List.

avatar image TurboHermit · Aug 05, 2012 at 08:47 PM 0
Share

I can't really find anything about Lists though... How can I declare a list of gameobjects ins$$anonymous$$d of an array then?

avatar image Eric5h5 · Aug 05, 2012 at 09:10 PM 0
Share

See the $$anonymous$$SDN docs.

avatar image Eric5h5 · Aug 05, 2012 at 09:37 PM 1
Share

Yes it does. Although the syntax is slightly different...

 List.<int> //Unityscript

vs

 List<int> //C#

Almost everything in the $$anonymous$$SDN docs for .NET applies to Unityscript (and Boo for that matter).

Show more comments

2 Replies

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

Answer by Eric5h5 · Aug 02, 2012 at 07:06 PM

You can't use RemoveAt with arrays. Use List instead.

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 TurboHermit · Aug 05, 2012 at 10:11 PM 0
Share

Oh yes, I've got it working now.

If found this very helpful for those whom are interested. http://forum.unity3d.com/threads/79760-How-to-use-generics-in-unity-javascript

avatar image
0

Answer by Jerdak · Aug 02, 2012 at 07:10 PM

Question needs more code. At the very least include P1Deck's declaration.

But since you're getting a 'not a member of GameObject[]' error it sounds like maybe P1Deck was implemented like:

  var P1Deck: GameObject[] = new GameObject[1];

In which case you would need to modify it in to a Javascript array that does support RemoveAt.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Is Random.Range really uniform? 1 Answer

Random Range Seems... Unrandom 1 Answer

Store multiple random integers in an array? 4 Answers

[Beginner] Using Random.Range in initial game object position 1 Answer

pick a random int with the value of 1 from an array 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges