x


How to use Array?

Hello,

I am trying to make a simple memory game, I got a couple of boxes you need to press however I want unity to choose the pattern (for example; It start with 1, you hit box 1 than it shows 1 and 3, you hit 1 and 3 etc etc).

However how I create such thing using Array? I am fairly new to Arrays so a helping hand would be great.

Thanks

more ▼

asked May 10 '12 at 01:52 PM

Bloodsail gravatar image

Bloodsail
2 3 3 3

Is this like a 'simon says' game? I would use a List for this, storing the index of the boxes.

Don't be tempted to use the UnityScript 'Array' class in the Unity API. It's really not good. You have all of the .net library to play with, after all.

May 10 '12 at 01:57 PM syclamoth

Yeah It is like Simon says, haha.

And what should I use instead of an Array? I am a beginner with Jc and so far what I've read on the webs they where using Arrays. (I can remember that I did something like this a while ago in Flash by using an Array :P)

Could you maybe make a little tutorial on how to make a function like this? I am kinda stuck lol.

May 10 '12 at 02:17 PM Bloodsail

You are not ready to make your own thing. Learn other people's things first.

May 10 '12 at 02:21 PM Jessy

@Jessy, What do you mean with your comment? :|

May 10 '12 at 04:50 PM Bloodsail

This is very basic stuff, and many people have tutorials covering it. You should go study the success they had, before attempting it yourself, or you'll end up wasting too much time reinventing the wheel.

May 10 '12 at 04:58 PM Jessy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Firstly, you will have an array that looks like this in C#:

public GameObject[] blocks; // you should use the editor to add all of the blocks to this array

This will store the GameObject for each block you have to press. You can access each block by its unique index like this:

GameObject currentBlock = blocks[currentIndex];

Keep in mind indexes start from zero, so if you have 7 blocks, they will be indexed 0-6.

Use a List to represent your "memory" chain. This allows you to add one integer at a time to the List, then get them back in order using a "foreach" loop. Using the List looks like this:

using System.Collections.Generic; // don't forget to put this at the top of your file

List<int> memoryList = new List<int>();

...

memoryList.Add(3); // adds the index 3 to the front of the list

...

// This will print out the elements of the list in the order they were added.
foreach (int i in memoryList) {
    print("The next number is " + i)
}

Hope this helps! By the way, does anyone know how to create separate blocks for code?

more ▼

answered May 10 '12 at 02:30 PM

Swift_On_Fire gravatar image

Swift_On_Fire
101 3 6

Thanks for the comment, will take a look into that :)

May 10 '12 at 02:35 PM Bloodsail
(comments are locked)
10|3000 characters needed characters left

Found a great tutorial that will help me a lot! Thanks to @agosyclamoth for the name of the actually game I was trying to make. It appears to have a good tutorial :).

(It's in C#) http://forum.unity3d.com/threads/113371-(Free)-Full-Game-programming-tutorial-from-start-to-finish-using-Unity3D.

more ▼

answered May 11 '12 at 09:46 AM

Bloodsail gravatar image

Bloodsail
2 3 3 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1355
x100

asked: May 10 '12 at 01:52 PM

Seen: 631 times

Last Updated: May 11 '12 at 09:46 AM