- Home /
Strange Error in JScript when using "delete"
Hi! I ve got a really strange Error in this Inventory Script when using the "delete" Statement. If i click the button it should throw out the item ( if there is any item in this slot) an should delete this item. So i thought i could use "delete InventoryItem;" and it worked! But after i restarted my PC i got this error when running the Script again : inv2.js(106,38): UCE0001: ';' expected. Insert a semicolon at the end. I tried everything ( at least i think so ) to get it to work again .... but without success. Has anyone a idea how to get this to work again ???
var inventory : Array;
var ItemObject_1 : GameObject; var ItemTexture_1 : Texture2D;
var ItemObject_2 : GameObject; var ItemTexture_2 : Texture2D;
public var emptyTex : Texture;
public var inventorySizeX = 4; public var inventorySizeY = 5;
var iconWidthHeight = 40; var spacing = 4;
public var offSet = Vector2( 100, 100 );
class InventoryItem { var worldObject : GameObject; var texRepresentation : Texture2D; }
function Awake() { inventory = new Array(inventorySizeX);
for( var i = 0; i < inventory.length; i ++ )
{
inventory[i] = new Array(inventorySizeY);
}
}
function OnGUI() {
var texToUse : Texture2D; var currentInventoryItem : InventoryItem;
if(PlayerGUI.doWindow)
{
for( var i = 0; i < inventory.length; i ++ )
{
for( var k = 0; k < inventory[i].length; k ++ )
{
texToUse = emptyTex;
currentInventoryItem = inventory[i][k];
if( inventory[i][k] != null)
{
texToUse = currentInventoryItem.texRepresentation;
}
var it = GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
if (it)
{
if(texToUse == ItemTexture_1)
{
Instantiate(ItemObject_1,transform.position,transform.rotation);
//This is the Problem (Without this line the Code is working):
delete InventoryItem;
}
if(texToUse == ItemTexture_2)
{
Instantiate(ItemObject_2,transform.position,transform.rotation);
}
}
}
}
}
}
function AddItem( item : InventoryItem ) {
for( var i = 0; i < inventory.length; i ++ ) {
for( var k = 0; k < inventory[i].length; k ++ )
{
if( inventory[i][k] == null )
{
inventory[i][k] = item;
return;
}
}
}
}
function AddItem( worldObject : GameObject, texRep : Texture2D ) { var newItem = new InventoryItem();
newItem.worldObject = worldObject; newItem.texRepresentation = texRep;
AddItem( newItem ); }
Answer by Magistrix 1 · Aug 18, 2010 at 09:24 AM
hi ok thx for the quick answers. i fixed this problem with
inventory[i][k]= null;
:) now it is working.
Answer by Wolfram · Aug 17, 2010 at 04:16 PM
InventoryItem is your class name you can't delete that. Use
delete currentInventoryItem;
(or whichever object you want to delete).
My guess is it didn't compile, even before the reboot, but one often overlooks these errors, and Unity will start normally - with the previous successfully compiled version of the script.
Answer by Magistrix 1 · Aug 17, 2010 at 05:36 PM
I tried
delete currentInventoryItem;
and i tried
delete inventory[i][k];
before but without success.. I tried it again but no change in the error log.
Answer by Eric5h5 · Aug 17, 2010 at 08:12 PM
As far as I know there is no "delete" in Unityscript. If you're trying to remove an element from an Array, use RemoveAt.