0

I'm new to Unity.

I have an array of GameObjects which I declared using

var shades:GameObject[];

Now, I had to convert this to an Array using

var allShades =new Array(shades);

but it seems like the gameObjects have turned in Objects because I can no longer use, for example

allShades[index].GetComponent(MyScript);

It gives me this error: 'GetComponent' is not a member of 'Object'

I'm not even sure of all the things that I have done here, hehe!

I have been googling but I can't find the solution for this.

Any help Pls?? Thank you! :)

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Rian
  • 171
  • 16

1 Answers1

0

No, you cannot use .GetComponent because allShades is an array (and an Array is an Object) and not a GameObject. You need first to access the GameObject inside it using allShades[index] and then you can use .GetComponent

See Unity Reference about Arrays too.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • @ Marco Acierno - Hi! Thanks for your reply :) But I actually used an index. And still get the error. The example in my post (`allShades[index].GetComponent(MyScript);`) was incorrect. I just updated it :) I have fixed my problem by using an array of integers which represent the elements inside `shades` array. But I think that the solution is too dirty. I believe there is another way. – Rian Aug 15 '14 at 01:08
  • You want to use an Array of Array? Then it's allShades[index1][index2]. I don't noticed that shades was an array – Marco Acierno Aug 15 '14 at 14:20
  • No, I don't think I have to use an array of array (or a 2-dimentional array, I guess?). What I am trying to do is to randomly access the elements inside my array shades one by one and use each in a game challenge. I have to record which elements have been used so it won't be repeated in the next challenges. I made it that way so that I can save in the database which elements have been used when the player saves the game, and then retrieve them when the user resumes the game. – Rian Aug 18 '14 at 00:17
  • I thought about creating a copy of the array and sort it randomly but in that case, I will have to save the sorted array in the database, which I don't know how to do. – Rian Aug 18 '14 at 00:23