0

In my game, players shall be able to select units from a menu, which will later be used (placed) in various scenes.

For that, I want to save the unit prefabs in a static array through code.
I then want to access these prefabs, to display some of their variables declared in attached scripts (like name, power and a thumbnail texture) to display on the UI. Later, I want to instantiate them into the scenes.

So far, I fail to save these prefabs to the array.

My code:

//save to array
if (GUI.Button(Rect(h_center-30,v_center-30,50,50), "Ship A")){
    arr.Push (Resources.Load("Custom/Prefabs/Ship_Fighter") as GameObject);
}

//display on UI
GUI.Label (Rect (10, 10, 80, 20), arr[i].name.ToString());

From the last line, I get this error:

<i>" 'name' is not a member of 'Object'. "</i>

So, where is my mistake? Did I forget something or declare it wrong, or is my approach here invalid to begin with (i.e, prefabs can't be saved/accessed this way; another type of list would fit this task better).

user3071284
  • 6,955
  • 6
  • 43
  • 57
Tuxedomask
  • 53
  • 2
  • 6

1 Answers1

0

You declared the array without type. You can either declare the array as a list of GameObjects or cast the elements when you extract them.

Example of type casting:

GUI.Label (Rect (10, 10, 80, 20), ((GameObject)arr[i]).name.ToString());    

// which is equivalent to
GameObject elem = (GameObject)arr[i];
GUI.Label (Rect (10, 10, 80, 20), elem.name.ToString());

Example of using a generic list:

// Add this line at the top of your file for using Generic Lists
using System.Collections.Generic;

// Declare the list (you cannot add new elements to an array, so better use a List)
static List<GameObject> list = new List<GameObject>();

// In your method, add elements to the list and access them by looping the array
foreach (GameObject elem in list) {
    GUI.Label (Rect (10, 10, 80, 20), elem.name.ToString());
}

// ... or accessing by index
GameObject[] arr = list.ToArray;
GUI.Label (Rect (10, 10, 80, 20), arr[0].name.ToString());
redent84
  • 18,901
  • 4
  • 62
  • 85
  • First off, thanks for the quick reply. Secondly: I am using UnityJS, not c#. Hence, i used the JS Array, which has no declared type (i unsuccessfully looked for a way to define one).
    So in order to get a defined type, i switched to a Generic List, which at least produces no errors in the editor.
    However, when i run the scene, and press the button, the gameobject added to the list is empty, resulting in an error when the script tries to access its name.
    So apparently, the way i save the prefab to the list isn't working. Where am i going wrong?
    – Tuxedomask Jan 29 '15 at 11:35
  • @user3802640 The `c#` tag confused me. Anyway the error is because you are trying to access the `name` property of a `Object` class (that doesn't have that property). Casting the element of the array to `GameObject` should solve your issue. – redent84 Jan 29 '15 at 11:38
  • What i do now: **Inserting** `arr.Add (Resources.Load("Custom/Prefabs/Ship_Fighter.prefab") as GameObject);` **Reading:** `GUI.Label (Rect (10, 10, 80, 20), (arr[i] as GameObject).name.ToString());` Still, the gameobject created remains empty and i get this error: _"Object reference not set to an instance of an object"_ However, when i drag the prefab onto the empty gameobject in the inspector, the name is displayed as intended. I guess there's something wrong with the way I let unity read the prefab. – Tuxedomask Jan 29 '15 at 12:21
  • @user3802640 That's a different error, before you got a *compiler* error, now you get a *runtime* error, because the object that you are adding to the array is `null`. This usually means that the object is not being loaded correctly. Check that the Prefab is loaded by saving it into a variable first. Remember that you can also use step-by-step debugging in Unity to inspect the variable content. – redent84 Jan 29 '15 at 12:40