C# Alternative for .ToBuiltin(Transform)

Does anyone know if there is a C# alternative for Unity’s JavaScript .ToBuiltIn function?

I am manipulating an inventory script that places an object into the array. Here is the JavaScript code:

var Contents : Transform[ ];
Contents = newContents.ToBuiltin(Transform);

Thanks for your help!

Usually if you want to add to an array, you just use Array.Add();

http://docs.unity3d.com/355/Documentation/ScriptReference/Array.Add.html

I know full well that has a C# alternative.

Yes - but it won’t allow me to pass in Transform into that function. I need to pass in Transform.

Transform[] newContents;
newContents = new Transform[Contents.Length];
for (int i = 0; i<Contents.Length; i++)
{
	newContents[i] = Contents[i];
}

So now how would I set Content equal to newContents.ToBuiltin(Transform) given the above code?

Better yet, this is what I am trying to convert to C#:

var Contents : Transform[ ]; //The content of the Inventory

// Add an item to the inventory.
function AddItem(Item:Transform)
{
var newContents = new Array(Contents);
newContents.Add(Item);
Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array

playersInvDisplay.UpdateInventoryList();
}

Use a List

http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.90).aspx

Could you elaborate? I have tried this before and I’m not understanding it.

List newContents = new List();
newContents.Add(Item);

//Contents = newContents.Add(Transform); ???

Make Contents a List instead of Transform[ ] and then just Add() to it directly. No need for another collection.

Is this what you mean?

List Contents = new List();

// Add an item to the inventory.
public void AddItem(Transform Item)
{
List newContents = new List(Contents);
newContents.Add(Item);

Contents = newContents.Add(Transform);

Inventory_Display playersInvDisplay = GetComponent<Inventory_Display>();

playersInvDisplay.UpdateInventoryList();
}

No - why are you making a second list?

public void AddItem(Transform item)
{
    Contents.Add(item);
}

No, with a List there is no need to replace the field, you can simply say

List<Transform> Contents = new List<Transform>();

public void AddItem(Transform item){
     Contents.Add(item);
    // Update your inventory list here; I'd cache the Inventory_Display component in Awake instead of using GetComponent every time you need to use it
}

Okay. Thanks for your help! I think I found why my inventory isn’t updated. How would I access a variable from another class?

void DisplayInventoryWindow() {

Pick_Ups item = GetComponent<Pick_Ups>(); // the class I am trying to access ----- trying to

print ("Item " + item.itemIcon); // the variable I am trying to access (it returns null)

}

This is the JavaScript code I am trying to duplicate

for(var i:Transform in UpdatedList)
{
var item=i.GetComponent(Item); /// I don’t understand how I would do this in C#?

print("Item " + Item);
print("Icon " + item.itemIcon);
}

Your 2 code blocks are doing different things. The JS code gets an Item component from each Transform in UpdatedList. Your C# code gets a Pick_Ups component on the same GameObject as the script executing that code.

for(var i:Transform in UpdatedList)
{
var item=i.GetComponent(Item);

print("Item " + Item);
print("Icon " + item.itemIcon);
}

is

foreach (Transform i in UpdatedList)
{
Item item = i.GetComponent<Item>();

print("Item " + Item);
print("Icon " + item.itemIcon);
}