Another Unity 1.7 -> 3 Script problem

Can anyone assist with the following? Worked in Unity 1.7 but does not work in Unity 3. I’ve checked the scripting reference for arrays and there doesn’t seem to be anything that’s changed.

var Contents : GameObject[];

function AddItem(Item : GameObject) {
	var newContents = new Array(Contents);
	newContents.Add(Item);
	Contents = newContents.ToBuiltin(GameObject);
}

This produces an error:

(on the final Contents = … line)

either its an inbuilt array ([ ]) or it is a Unity Array instance. But it can’t be both.

So either change it to

var Contents: Array;

or change the constructor to

var newContents = new GameObject[someLength];

But this is something which has changed yes? The code works perfectly in 1.7 and it’s actually quite close to an example given in the Unity manual when they are discussing arrays.

This is the code suggested on the Array page of the unity scripting reference, but it does not work in the f3 beta of unity 3.

	var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
    array.Push(Vector3(0, 0, 2));
    array.Push(Vector3(0, 0, 3));

    // Copy the js array into a builtin array
    var builtinArray : Vector3[] = array.ToBuiltin(Vector3);
    
    // Assign the builtin array to a js Array
    var newarr = new Array (builtinArray);
    
    // newarr contains the same elements as array
    print (newarr);

The error is: BCE0022: Cannot convert ‘System.Array’ to ‘UnityEngine.Vector3[ ]’.

Have they changed their policy on this in the new version? Is there a scripting manual out for version 3?

In my app, I build up an Array of vectors over time using myArray.Push(v), but then when I try to use myArray[0].x, it doesn’t know the type. I don’t seem to be able to cast the myArray[0] as a Vector3 either. Any ideas?

Does anyone know what specifically has changed between Unity iPhone 1.7 and Unity 3 which causes all of these typing/casting issues? (not sure about 2.6 - never really used it but it might be similar).

I’m still struggling to get all my 1.7 stuff running in 3 and it’s frustrating. Knowing why it’s happening would really help.

@geppetto: your code seems to compile OK here. Are you using the latest version of Unity? (You could try with the new 3.0 release :wink: )

No - doesn’t work in Unity 3 (i.e. final release) :frowning:

Sorry to bring this up again, but I’m still having heaps of problems getting old Unity iPhone code to work in Unity 3. Does anyone know if this might be a setting that I’ve accidentally turned on, etc? Example is in my original post - still something that’s stopping my project from compiling, but my Unity iPhone version works perfectly.

Use #pragma downcast.

–Eric

You could try this too:

    var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
    array.Push(Vector3(0, 0, 2));
    array.Push(Vector3(0, 0, 3));

    // Copy the js array into a builtin array
    var builtinArray : Vector3[] = array.ToBuiltin(Vector3) [COLOR=red]as Vector3[];[/COLOR]
    
    // Assign the builtin array to a js Array
    var newarr = new Array (builtinArray);
    
    // newarr contains the same elements as array
    print (newarr);

Optimally you would not use Array anyway anymore in U3.

Array is bad, either you need fixed size and direct index access, then use Vector3[ ] directly or you need to dynamically add, then use List. from System.Collections.Generic which can directly hand you a Vector3[ ] array.

Thanks for the hints.
I have a big script that I have to convert from array to list… Sounds like hell !