I’ve opened up a Unity iPhone project in beta 6 and I’m getting errors. Unity 3 doesn’t seem to like the way I’m doing some things but I don’t know why or how to correct it.
Here’s an example - all the issues are on code fragments similar to this:
private var persistence_ref : persistence;
function Start() {
persistence_ref = GameObject.FindWithTag("Persistence").GetComponent(persistence);
}
I have an empty game object in the scene with a script attached to it called persistence.js (and a tag of “Persistence”).
This all works perfectly in Unity iPhone 1.7, but Unity 3 gives me this error:
I’ve also tried doing this (there’s only one persistence component in the scene):
GetComponent returns a Component base object, not a persistance class. You need to tell the compiler that what you got was a persistance class and to turn it’s component reference into a persistence reference.
private var persistence_ref : persistence;
function Start() {
persistence_ref = GameObject.FindWithTag("Persistence").GetComponent(persistence) as persistence;
}
I’m at “The Other Job” now so I’ll have to wait until later this evening to try this out. I hope this works - it’s driving me insane. I have yet to get even one of my simple Unity iPhone projects to run properly in Unity 3.
Can anyone help out with that last one? I’m having trouble figuring out how I’m supposed to be typing/casting things in Unity 3. Seems like quite a lot of my code needs to be rearranged and I don’t really understand why.
I’m not familiar with Javascript, but it looks like you’re not casting the result again. FindObjectsOfType likely returns an array of objects. You would need to cast that to an array of RigidBodies, or at least declare it as such.
Sorry if you already knew that much and it was just the syntax you wanted. I’m strictly C# only, I’m afraid and you don’t need to cast the result in C# because there’s a generic version of GetComponent. Is there a Javascript equivalent in 3.0?
Yeah - more a syntax problem. I get that I have to cast the ‘objects’ as ‘rigidbodies’, just not sure how I do it in the context of that for loop.
The whole thing is really bugging me (no pun intended). Was assuming that I’d be able to open 1.7 projects in U3, the software would do the conversion of the project for me, and it would “just work”. I think it’s a bit crappy that I’ve got to go back and change 50+ lines of code that was all working fine in Unity iPhone 1.7
I think I got it! And now I’m posting back just in case any else is having this problem, though I suspect I’m the only dummy out there
This is what I ended up doing. Seems like overkill to me but it’s the only thing that would work:
var balls : Rigidbody[] = FindObjectsOfType(Rigidbody) as Rigidbody[]; // Seriously?!
for (var thisBall : Rigidbody in balls) {
var thisBallProperties : balloon_pop = thisBall.GetComponent(balloon_pop) as balloon_pop;
}