The following code will break a build in Unity 5 and emit a warning in the editor:
class MyComp : MonoBehaviour {
public string s;
void Awake() {
var c = GetComponent(s);
}
}
I am fully aware that this is a serious problem for the feature in IL2CPP + WebGL that strips out unused code from a final build. But does this justify to break existing code without having GetComponent(string) marked obsolete for at least a couple of months before?
What about a warning “The GetComponent(string) statement in line XXX, file YYY will disable optimizing your build size” and just throw everything in when using GetComponent anywhere in non-editor code?
Is that option too difficult to maintain for at least some 5.x versions (while you obsolete the current function)?
If you absolutely need it, you can always use reflection. I did this extension method for AddComponent, but you can do the same for GetComponent, just change AddComponent for GetComponent. This will be slower, so use only if you really need it.
I’ve had similar issues using AddCompnent(string). it has broken my entire game in Unity 5. I’m trying to get around it by first casting it to a type, however there seems to be this weird bug for me where the game goes into some infinite loop and just hangs when doing this (1/10 times). None of this happened in 4.6 or earlier. I do understand that there is a need to move the API forward, but this functionality is extremely useful to me and i really want to see it maintained.
heres my link. its a little different issue, but i think these two are related.
Yes. There is one single valid use-case: It is currently used and never had been deprecated.
That might not be the most thrilling reason, and there are (nearly) perfectly working alternatives which are almost always faster more type save and enable additional features.
But it is still a currently used feature and fixing code needs time. So if its easy to not break code, than I suppose to not break code now.
I am not suggesting to keep this function forever, but make it “Obsolete” for a couple of versions and then remove it.
Edit: By the way, it is still not marked Obsolete in the just released Unity 4.6
It is a major release. It is the perfect time to get rid of that kind of feature. Major releases always mean that you need to take the time to upgrade your project.
But I don’t really understand what could prevent you from just updating your code? It should be almost a no brainer.
Yes, I agree. An earlier major versions are good candidates to just break stuff if its necessary. I just wanted to say that: if it might not be necessary, just don’t do it now without a warning period.
Thing is… it seems to me that the Devs went through a lot to keep things as compatible as possible for Unity 5 release. They got a very tricky auto-updating system to keep as much code compatible as possible, even patching DLL’s on the fly.
For this GetComponent overload, they choose another approach. They allow the function call when the editor compiles, but it is a compile error during building when used in non-editor code. That seems a bit risky to me.
So my suggestion is: Why not save the trouble of this setup and just deprecate the function, and maybe emit a warning that the DLL-size optimization will not kick in if the function is used to raise awareness. Then drop it in a later 5.x if it keeps bloating the code base.
But I am not sure about the background of this function… It may be that they changed the backend so much that providing this functionality in the player needs such an amount of hacks or need a lot of new code to be written that its just not as easy to keep it in.
First, its not my code that needs changing. There are several asset store plugins we are using (e.g. PlayMaker) that need to be fixed.
And second, I am suggesting this on behalf of other users as well. I am very grateful that the Devs seem to think often about other people or else they would just have removed things like the “GameObject.animation” property and let people have fixed their code by themself.
I like this auto-upgrading approach (although I definetely would have facepalmed in the meeting where this idea came up the first time ). And I can see that it can not so easily be done with GetComponent(string). But I don’t like the solution they choose for this problem. That’s why I express my concerns here, ask for background information and provide an alternative.
Unity 5 is still in beta. Give them time to update their code.
Of course they try to make it as flawless as possible. But I still don’t understand why this is a problem. If you make a major update, you need to take time and you need the people who can do it.