Getting Components - Code Problems In Beta

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):

persistence_ref = FindObjectOfType(persistence);

And that one gives me:

Can anyone help?

I’m just hazarding a guess so I apologize if it doesn’t work but try

private var persistence_ref : Persistence;

function Start() {
   persistence_ref = GameObject.FindWithTag("Persistence").GetComponent(Persistence);
}

Also maybe try renaming the script to also have a capital P. Without having Unity 3 I have no way of testing it I’m affraid :frowning:

EDIT:

Here’s another idea too.

private var persistence_ref : persistence;
private var persistenceObj : GameObject;

function Start () {
    persistenceObj = GameObject.FindWithTag("Persistence");
    persistence_ref = persistenceObj.GetComponent(persistence);
}

I still would recommened capitalising the name as Classes should start with a capital but check to see if it works if you’d rather not.

Try that.

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.

@Ntero:

That gives me the following error:

Does anyone know what it is in Unity 3 that’s changed (when compared to Unity iPhone) that’s causing these sorts of problems?

Seems like a typing thing, but if anything I thought Unity iPhone was MORE restrictive in that way.

var urMom : Transform = GetComponent (Transform) as Transform;

I don’t know who the bright spark was that made it this way, but it’s kinda lame. :confused:

OK, so maybe:

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.

Is your script named “Persistence” or “persistence”? Proper capitals are necessary.

:frowning:

All my scripts are named lowercase - so “persistence” is correct.

At least the other job gives me time to read through the Unity forum :slight_smile: That’s better than nothing I guess…

OK. That stuff is working, but now I’ve hit another example (works in Unity iPhone) and I can’t get my head around the change on this one.

var balls = FindObjectsOfType(Rigidbody);
for (var thisBall : Rigidbody in balls) {}

Gives me (against the second line - the for loop):

Is it that I’m just doing things in a “bad” way in the first place?

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

Sorry - grumpy today.

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 :slight_smile:

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;
}

Yeah, seems kinda dumb. But I bet there are some optimizations that make use of it, and make your code run faster. :slight_smile: