I am trying to use a ‘struct’ or rather a class I’m treating as a struct. The code compiles fine; however, when it is run, Unity is giving me an InvalidCastException when instantiating the new struct.
// My 'Struct'
class WaveStruct extends System.ValueType {
var types : Array;
var start : Vector3;
var peak : Vector3;
var stop : Vector3;
var rotation : float;
var speed : int;
}
// Instantiating the struct
var tmp : WaveStruct = new WaveStruct();
tmp.types = types;
. . .
I have tried extending System.Object and not extending anything (I assume it defaults to MonoBehaviour?). Is this an issue w/ defining sub-classes and trying to use them within a parent class, something with how Unity’s JS is handled, or just something basic that I’m overlooking?
Thanks for the reply. Yes, I’m using Unity iPhone Advanced 1.0.3. I was not originally including System (I think I was under the impression it was included by default); however, adding that to the top of my script still results in the same InvalidCastException that I was getting before.
Also, using:
class WaveStruct extends UnityEngine.Object { ... }
That is quite odd. I’d recommend first upgrading your Unity iPhone version, as it’s out of date.
If you still experience it after that: do you get the same result if you put the code snippet you posted (while using ‘extends UnityEngine.Object’) into a new project? If you do, I’d file a bug report, with that new project attached. If you don’t, the cause of the problem is likely outside the code you posted here.
As a side note: there is only one relevant difference between classes and structs in .NET. Instances of classes are passed and stored by reference, instances of structs are passed and stored by value. As far as I’m aware, it is not possible in Unity JS to create objects with ‘pass by value’ behavior, which makes me confused at what you are trying to do here. You will need to use C# if you want structs.
I did end up using C#. I did not want to upgrade to Unity iPhone 1.5 as there are some known compatibility issues with modules I am using in my current project. My project is mostly written in JS, and as such I was trying to keep all aspects within the same language. The project is nearing completion, which is another reason not to upgrade, but after that I will try and see if I can get it working, and if not, submit a bug report.
As far as what’s possible in Unity JS, as I stated initially, I’m not extremely familiar with what it can and can not do. I appreciate the comments.
Actually, you know more than I do. I just looked up System.ValueType, as I had never heard of it ;). I guess that in theory, your original plan should work. Not that that observation brings us closer to why it doesn’t :P.
i also checked the msdn now. As far as I see there wasn’t a .NET 1.1 version of this outside the compact framework so what you migth actually be seeing is the fact that it does not exist or at least not the way you think as unity iphone, unlike unity desktop, is .NET 1.1 only
The import only pulls the types into the local namespace. You can always refer to something by its full name i.e. “System.ValueType” without importing it. The only advantage of using an import is that you can refer to it just as “ValueType” instead of “System.ValueType”.