I’m a bit noobish. I was translating a java script to c# and I’m getting 3 errors dealing with my Contents and newContent arrays. These 3 errors are the exact same for line 23. Can someone please help me?
- error CS0119: Expression denotes a
type', where a variable’, value' or method group’ was expected
- error CS1502: The best overloaded method match for `System.Collections.ArrayList.ToArray(System.Type)’ has some invalid arguments
- error CS1503: Argument
#1' cannot convert object’ expression to type `System.Type’
The script -
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
Transform[] Contents;
public void AddItem ( Transform Item ){
ArrayList newContents = new ArrayList(Contents);
newContents.Add(Item);
Contents = newContents.ToArray(Transform);
}
public void RemoveItem ( Transform Item ){
print("made it here");
ArrayList newContents = new ArrayList(Contents);
int index = 0;
bool shouldend = false;
foreach(Transform i in newContents){
if(i==Item){
newContents.RemoveAt(index);
shouldend = true;
}
index++;
if(shouldend){
Contents = newContents.ToArray(Transform);
return;
}
}
}
}
gjf
2
not sure what you’re trying to do with the remove method, but you could make this much simpler by using List. keep your 2 using statements, but add this:
using System.Collections.Generic;
then:
public class Inventory : MonoBehaviour
{
private List<Transform> _contents;
void Awake()
{
_contents = new List<Transform>();
}
public void AddItem(Transform item)
{
_contents.Add(item);
}
public void RemoveItem(Transform item)
{
var index = _contents.IndexOf(item);
if (index > -1) // found?
{
_contents.RemoveAt(index);
}
}
}