Trouble working with a Custom class, JavaScript.

Another very important question. The scenario is this:

Player interacts with object to ‘collect’ it → Object is an instance of a Prefab → Object sends info about itself to player Inventory script → Object deletes itself as if ‘collected’ → Player instantiates said object later.

Edit(5/1/14): Getting the prefab via code is no longer a problem. (Originally I couldn’t figure out how to have the item send it’s own prefab to the inventory as a variable.) I’ve set up a custom Class that will hold each item’s info, and I can simply drag the item’s prefab to the correct spot on that item, then pass that variable of the new class to the inventory.

The issue I’ve run into now is trying to let the script set that class’s variable. Unity’s throwing me a lovely “Type ‘ItemInfo’ does not support slicing.” As I understand it, this has to do with #pragma strict, and while removing that will help, it can cause issues down the road. Here’s what I’m trying to do:

New Class:

public class ItemInfo{

	var Name : String;
	var Tag : String;
	var PFB : GameObject;
	var Amount : int;
	var MaxStack : int;
	var Weight : float;
	var Durability : float;
	var Level : int;
}

Item Script:

#pragma strict
public var info : ItemInfo;

function Start () : void
{
	info[0] = "Test Sword";//Name
	info[1] = this.gameObject.tag;//Tag
	//info[2] is prefab, will assign through inspector
	info[3] = 1;//Amount
	info[4] = 1;//MaxStack
	info[5] = 5.5;//Weight
	info[6] = 100;//Durability
	info[7] = 1;//Level
}

Should I be assigning these values some other way? Like info.Name = “Test Sword”; ?

If you don’t want to use drag and drop, you have two other choices:

  1. You can place the object in the Scene. It may be hidden by turning the renderer. And you may have to disable other components like the colliders. You can then use GameObject.Find() or GameObject.FindWithTag() to get access. You then use Instantiate() to make copies, and activate the renderer and any other components. Note you cannot deactive the object, because GameObject.Find() does not find deactivated objects.

  2. You can place the prefab in the Assets/Resources folder and loading it this way:


  GameObject shipPrefab = Instantiate(Resources.Load("Spaceship")) as GameObject;

Yep. info.Name = “NameHere”;

Guess I had to type it out to think it through. ^_^;