Search Unity

Build fails to find generic extension method while the code runs fine in the editor

Discussion in 'Scripting' started by VacuumBreather, Feb 24, 2020.

  1. VacuumBreather

    VacuumBreather

    Joined:
    Oct 30, 2013
    Posts:
    68
    I have defined the following extension method to check whether objects are alive and valid.
    In the editor, playmode etc everything works fine. But as soon as I try to build a standalone
    Unity complains that my MonoBehaviors and ScriptableObjects as well as Camera and other types that derive from UnityEngine.Object do not contain a definition for CheckedRef.

    What am I missing here? As I said, it works fine in the editor and Visual Studio doesn't report any issues either.

    Code (CSharp):
    1. namespace XYZ
    2. {
    3.     using UnityEngine;
    4.  
    5.     public static class ObjectExtensions
    6.     {
    7.         public static T CheckedRef<T>(this T obj)
    8.             where T : Object => IsValid(obj) ? obj : null;
    9.  
    10.         public static bool IsInvalid<T>(this T obj)
    11.             where T : Object => !IsValid(obj);
    12.  
    13.         public static bool IsValid<T>(this T obj)
    14.             where T : Object => !(obj is null) && obj;
    15.     }
    16. }
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    your using should go outside of the namespace. maybe because of that.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    1) you have you using statement defined in an odd location. While yes it can be put there, it also has implications when put there (it has to do with namespace conflict ordering)

    2) Where is this script located? When Unity compiles the library it ends up in depends on the folder its in. For instance if you have it in an 'Editor' folder it won't be accessible at runtime.

    3) You don't have any defines around it like #if UNITY_EDITOR ???
     
  4. VacuumBreather

    VacuumBreather

    Joined:
    Oct 30, 2013
    Posts:
    68
    1) I am perfectly aware of the location of the using statement and it's there on purpose. Among other things it's even a StyleCop rule to put it there.
    2) It's located in a normal scripting folder. No special Unity Folder
    3) No I have not
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    pfft man, next time answer your own questions if you're so full of air

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive

    in other words, there is no need for the assembly to be included, because you probably don't refer to it by namespace anywhere. IDE is not a compiler. maybe there is a switch for a compiler, but in any case, this is 99.99% the source of your problem. [edit] in fact, to be precise here, it's not because of this one, but because of 'usings' you nested in other places [/edit]

    besides, you don't need those extensions in the first place. unity already does a similar thing with the Object type anyway

    you could already check for null by going
    if(something)
    where something derives from Object.
     
    Last edited: Feb 24, 2020
  6. VacuumBreather

    VacuumBreather

    Joined:
    Oct 30, 2013
    Posts:
    68
    Right. Apparently everybody knows this so well around here that they neither know the reason why using statements should be inside a namespace, nor could you explain it, nor do you even try to understand my methods and what I might want them for.
    Also they work fine. As expected a Unity bug in the build system reported a false positive error when the issue was a completely different one. But nice answer people. Last time I ever try to get help here.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I know why they're used. Though... it's not a matter of "should" it's a matter of "could". They shouldn't necessarily be there unless you need them to be. It's a matter of ordering how the compiler looks up a namespace. So if you have a namespace conflict by having something on the inner portion of the namespace the compiler will search there FIRST before the ones outside.

    As for preferring one or the other. Well putting them outside of the namespace is the norm. But the actual difference (if you have all in one place or the other) is inconsequential. There is no specific place that is actually required. Either way works, and is why I phrased 1) the way I did.

    Wow bro... you come off as a condescending and entitled prick.

    I attempted to help you. But there's not enough information yet. So I asked some questions in regards to your situation. You know... some basic/common gotchas that average users run into that MIGHT have been your root cause. (yes my (1) was not a cause either, I was just pointing it out as some people do that purely accidentally. I wanted to gauge who/why you might have. I got the reason from your response.....)

    We don't have access to your machine and your setup. The problem could be caused by 100 different things... but some of those things are more common than others. Why would I guess that thing that happens 0.00001% of the time when it's more likely it's the thing that happens 80% of the time. If they weren't the issues we could than start narrowing down further with follow up questions (like checking your build settings to see the target framework/C# version).

    I don't know you... you have 56 posts and nothing in your OP demonstrates your skill level.

    So we made NO ASSUMPTIONS about your skill level, we attempted general questions that MIGHT lead in the right direction, and you got offended.

    We're here offering our assistance freely out of the kindness of our hearts and because we like the community effort. Sorry if that wasn't enough for you.

    Good luck with that attitude.
     
    Last edited: Feb 24, 2020
    orionsyndrome likes this.
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    it's your attitude that's the problem. also good riddance.
     
    lordofduct likes this.
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It's not surprising to me that KeldorKatarn is upset, since orionsyndrome has been a clueless jerk in this thread. (Opened with a naive and easily-disproved theory, then doubled-down on it with rude technobabble that only shows he doesn't understand the issues.)

    But generalizing from that to the whole forum seems hasty. Lordofduct suggested a couple of things to check that could hypothetically have led to the described behavior, and since it turns out the true problem was actually caused by something not mentioned in the OP, that's probably the best that anyone could have done under the circumstances.
     
    lordofduct likes this.
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I did just notice orionsyndrome's follow-up post was a bit rude. So yeah, fair game there.

    But yeah, no reason to extrapolate from there onto the rest of the forum.

    And I'd argue just because someone is naive with their post doesn't mean anything to the forum as a whole. Just as we can not assume OP's skills, we can't not expect various levels of knowledge to pop up when you ask a question. To someone who has only ever seen the using statement outside the namespace, it's not completely strange to think that syntactical oddity relative to your own experience might be a source of some issue.
     
  11. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    at this point I'm wondering if we're reading the same thing...
    what rude technobabble? a quote from MSDN?

    because I said to a guy that he was full of air because he was like this the whole time?

    something tells me won't be friends on this forum.
     
  12. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Yeah, for someone unfamiliar with that usage, that's a valid guess (if not terribly insightful). But if you're picking that solely on the grounds that it's different than what you're used to, then after the OP says "I did that on purpose for a specific reason", you should stop guessing and either (a) trust the OP understands it better than you do, or (b) do some careful research to confirm your suspicions. You don't come back and say "I'm 99.99% sure that the problem is that you are doing this thing...in some hypothesized code that you haven't posted...which causes your symptom by some unspecified mechanism!"
     
  13. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    To conclude I'm 99.999% sure you're a dichotomy of your online persona in real life. Read the advice above, let it sink in, marinade then move on. You obviously know your stuff. No need for the bravado.
     
  14. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    This is really unnecessary, all of it, I opened up with a naive suggestion, granted, but this forum is 99% consisted of problems that have naive solutions. You cannot blame me for that.

    And then another person comes in and offers the same kind of suggestion.
    So you have to give it some credit right? It wasn't just a random technobabble.

    And then I go over to check if I'm too naive or what, and you know what -- it does matter where you put your using directive. "Unspecified mechanism" is quoted right there. I'm not sure if you've seen that quote or if you're just doing this for fun.

    So in the meanwhile I get to see "I am perfectly aware of the location of the using statement and it's there on purpose." Oh right? And it did not occur to you to politely mention that before you got pissed because we're a bunch of naive forum mites??

    No I'm not using StyleCop. I'm sorry for not being part of that fantastic Style Club that bends rules and expects everyone else to mysteriously read minds. It also makes no sense to ignore the last two sentences that I've also posted.

    Unity does overload a bunch of operators on UnityEngine.Object, I don't know if null checking is still a thing, but it still includes the implicit bool conversion, so the extensions speak to me about the level of his knowledge.

    When you come over and ask for XYZ, and that's all I can see and care about, and when I or anyone else proposes to you that you're not doing things by the book, I at least expect you not to be "yeah but I'm perfectly fine with my weird setup, you don't know what you're talking about, this is perfectly fine in lala-land because I'm from lala-land but forgot to tell you."

    So yeah, he's full of air. And I'm not trying to be rude, I'm just, like, offended by his lack of any perceivable manners.
    Take it or leave it.
     
  15. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    @unit_dev123 I know you've waited for this moment, but please man, just.. of all people, that's like the longest sentence I've seen from you. but let me tell you this, maybe you're right, and thank you.
     
  16. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Alright man, all of us have derailed this thread enough. Lets back up.
     
    orionsyndrome likes this.