Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

ArgumentException: Value does not fall within the expected range.

Discussion in 'Scripting' started by qcw27710, Oct 7, 2019.

  1. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    No compile errors, but as I press play I get error in the title, with Stacktrace pointing to this single file:

    Line referenced:
    myDict[myEnum.enum1] = this.myClass1.myFunction1;


    myDict is:
    Dictionary<myEnum, myDeleg> myDict;

    myDeleg is:
    public delegate void myDeleg(params object[] data);


    myEnum is existent (otherwise I would get a compiler error).
    myFunction1 is:
    public void myFunction1(params object[] data);


    What is happening and why?

    Edit: Weirdly enough this is one of three such commands, this one is the only one that causes an error, and it's the middle one. It's not last or first one. It's preceded and succeeded with near identical command.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    If you're doing this in 3 other places and only one of them is causing a problem, and you can't find the difference between them, then there's something you've overlooked. Given that, we're definitely going to need to see the actual code, not this obfuscated summary of it.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Debug.Log (myDict);
    Debug.Log (myEnum.enum1);
    Debug.Log(this.myClass1.myFunction1);

    (haven't tested, but you should get the idea)
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Based on this it looks like an ArgumentException could occur if either your key or your value isn't the correct type for the dictionary.

    My guess is that you've just summarized what you meant for your code to do and inadvertently left out some subtle mistake in the actual code.

    When debugging a problem, you generally want to copy/paste code that you have actually run and that you know is generating the error. You can make a simplified or obfuscated version if you want, but you should actually compile and run and test your modified version to ensure it has the same problem as the original.
     
    Joe-Censored likes this.
  5. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    No, they're literally same place. Same scope etc.
    Same script, same instance, in
    Awake()
    , next to each other.

    First:
    Debug.Log (myDict);

    System.Collections.Generic.Dictionary`2[myEnum,myDeleg]


    Second:
    Debug.Log (myEnum.enum1);

    Outputs string value of that enum. "First", "Second", "Third", will print Second.

    Three:
    Debug.Log(this.myClass1.myFunction1);

    Doesn't compile. Had to comment out to get two above. Visual Studio throws this:
    Argument 1: cannot convert from "method group" to "object".


    I posted that line. This isn't intricate programming, but a basic assignment of delegate to Dictionary whose key is an Enum, that fails for reasons unknown to me.
     
    Last edited: Oct 7, 2019
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    You misunderstood what I'm saying.

    The code you've posted here is not helpful in diagnosing the problem that you don't understand because it's been filtered through you, who as established by the existence of the thread, doesn't understand the problem. In the process of obfuscating your code, you have unknowingly obfuscated away whatever is causing the problem. The code you've posted no doubt shows your intent in the code that exists, but not the details of it, and the details of it are clearly what is broken.

    No one's trying to steal your code. It's not worth anything to us.

    Post the actual code, or we literally don't have the information needed to help you fix it.
     
    Joe-Censored likes this.
  7. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    This is the code, I only changed names.
    Is this:
    myDict[myEnum.enum1] = this.myClass1.myFunction1;

    So much more difficult than this:
    guiCommand[GUIAction.HideTooltip] = this.TooltipHandler.DisableHighlight;
    ?

    This is literally it, I don't know what else to give you. It's an Awake() function, and it isn't invoked by anything else. It's just right there. And it fails.

    Any other "code" I gave were only definitions (e.g. return types, accessors etc.) of each part of this line. So you can see what is a function, what access it has etc.
     
    Last edited: Oct 7, 2019
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,607
    Can you show us the actual offending code?

    Put it in code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    You seem to be transposing your code to the thread here, changing the names, and not showing the full context. Some things may be getting lost in that transposing, or in the surrounding context (is there a possible namespace/scoping conflict?)
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    Why is this like pulling teeth? You haven't given us enough information to help you solve your problem, that's the beginning and end of it. If you want your problem to be solved, then post the information that we're telling you is necessary in order to solve it.

    What you're doing right now is basically walking to the mechanic and saying "Hey, my car is making a sound like br-br-br-br-br, what's wrong with it?" And then when he says "Your impersonation of the sound doesn't tell me anything, can I look under the hood?" staunchly refusing and insisting that he diagnose it based on your imitation of the sound. The mechanic can't help you fix the problem if you hide the problem.

    What is your resistance to posting the actual code?
     
    Joe-Censored and lordofduct like this.
  10. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    It's literally that one line. It's an assignment from Awake(), nothing calls it, I don't understand what you and @StarManta want. I literally don't. It's an empty GameObject, it has a script attached. Its Awake function has three such assignments one of which fails. What part do you want me to share? If I click the error, Visual Studio opens this line, nothing else. Stacktrace is only one deep, which means there's no deeper cause.

    Where would I find the part of the code you need me to copy? I'm not refraining, but I don't get what you need. Which offending code? It's that one line according to Unity and Visual Studio. Stacktrace points exclusively to this line, nothing more nothing less. I don't know what other code is causing the issue. Console points me to this line only.

    If there's something else I need to share, I need to know what.
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    And I likewise literally don't understand what you don't understand about "post your code". If you're not sure what parts of context is important (and YES, context is absolutely important, no matter how shallow the stacktrace is), then just paste the whole script file here. If we only look at one line of code, there are about a trillion things that might be wrong on other lines that could cause this one line to trigger an error. If we see the rest of the script file, that should narrow down the possibilities and give us the information we need to be able to either solve it or know what questions need to be asked to get towards the answer.

    At this point this is just frustrating so unless you post your code I (and likely anyone else able to help you) am not going to bother trying to pull teeth anymore.
     
    lordofduct likes this.
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,607
    The reason is because we don't have the context of that code. There may be stuff going on around it that puts the dictionary into a state that causes it to throw this exception. Or when you wrote the line here and gave us information about the Dictionary you transposed that information incorrectly.

    Code is very specific, very particular about the EXACT syntax.

    So when we are given context, we are given more information, to get to the exactness of the problem.

    I'm not understanding why it's so hard for you to give us the context... you don't know the answer to your problem. We may. If you go to your doctor and say "my stomach hurts" and they ask you "what have you eaten this week?" would you respond "I told you what's wrong, I don't understand why you need more information!?"

    ...

    And then you give us a shady download link.

    You can post the code here... I gave you a link that shows you how to use the code tag.

    As I say to many new comers to forums looking for help... we offer up our assistance for free and at our leisure. Why should we put in more work answering your question than you put in asking the question?
     
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Unfortunately without seeing the actual code, I don't believe I would be able to assist. Can you post the full script and error message and match up the error line number with your posted code.
     
    qcw27710 likes this.
  14. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    I did post link to full code, zipped, but then one of fellas above responded that I should post it to Unity (Unity doesn't allow files that big), so I uploaded it to a hosting and posted link here, but then it was called "shady", implying lack of will to follow it. After some couple of minutes, I decided to rebuild core directives and I deleted post containing the link. I'll find another way to get things done.

    Issue Solved.
     
  15. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, please don't post external links. And a single .cs text file is "that big"? Not sure what you mean by "rebuild core directives". And it generally good forum etiquette to post the solution so others may benefit.
     
    qcw27710 likes this.
  16. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,607
    Note I called it shady because it was like "filedl dot io slash fdjklafdj" or something like that. These no name links are to download something, we don't know what that thing is we're downloading. That IMO is called a "shady link" as it could lead to anywhere.

    Furthermore we said post the code here... as in copy paste it in the thread. The code that is your class that has the offending line of code. I even included a link to a unity forum thread that explains how to post code to the forum. I'll post it again:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    These kinds of practices are the norm on programming forums. Programmers don't usually like links that could lead anywhere, we don't know what we're downloading from you. Programmers like nicely formatted code, so they can read and easily understand it (use code tags). We also like the context to code, because code is not as simple as a single line, lots of stuff can play into why you're getting an error... especially runtime exceptions which rely more on the state of the program rather than the exactly line of code.

    These are habits you may want to get familiar with if you intend to hang around programmer communities. They're just the norms.
     
    Darkwing4 likes this.
  17. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    I was told to provide context, well, there's multiple files involved with this file. So I had to include them all. I asked a couple times what parts of code do they need. I was told post offensive code, which is that one line, but that wasn't enough.

    I'll rebuild the way this code is shaped so I won't have to rely on this structure and won't have to see the error. That's how I "fixed" it.

    Anyways, lets end it here, lets not drag it on.
     
  18. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    So I downloaded that shady-looking zip file and had a look, and it looks like the issue (or at least an issue) is that he had a function that syntactically ought to have been returning a delegate, but it was calling the delegate and returning null instead. And as any of us could have predicted the line he was obfuscating was not like the first line (that functioned correctly) - though it was like the third line (which was never reached because an exception was thrown.

    Anyway this is why I usually don't persist in trying to get people to help me help them because wow what a waste of time this thread was.
     
    lordofduct likes this.
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,607
    note... "context" means the surrounding lines of code.

    Code (csharp):
    1.  
    2. public class SomeClass : MonoBehaviour
    3. {
    4.     public Dictionary<string, int> myDict = new Dictionary<string, int>();
    5.  
    6.     void Awake()
    7.     {
    8.         myDict["A"] = 5;
    9.     }
    10. }
    11.  
    (sidenote - see how readable this code in code tags is???)

    The context to: myDict["A"] = 5;

    is the entire class.

    By having this context I know what myDict is actually typed as (a Dictionary<string, int>), rather than going by your word it's typed a specific way. When you include code that's not actually the code like this:

    That's not the ACTUAL code, it's clearly renamed obfuscated code. You might say you've typed your Dictionary as those, or set your dictionary entry like that, but it's clearly NOT your code. And thusly we don't know what your problem actually is, since an exception like you deescribed could be the result of some minor thing like a typo that you were unaware of and didn't transpose when renaming your stuff and typing it here.

    So... showing your ACTUAL code is of help to us.
     
    Last edited: Oct 7, 2019
    JeffDUnity3D likes this.
  20. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Shaped? And "won't have to see the error"? So you're just ignoring it? Still not clear. This is for your benefit on your next issue!
     
  21. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you had just posted the code, this issue would've already been solved for sure. What you're trying to code up does clearly work, there must be a simple error in your code.

    My guess is, based on the information you provided, that you've just forgot to initialize something. A candidate for that would be
    myClass1
    instance in
    this.myClass1.myFunction1
    .

    The error you posted is common when you try to create a delegate instance from a variable pointing to null, i.e. type information is available by declaration, but no object has been assigned, for example
    Code (CSharp):
    1.  
    2. public class Sample
    3. {
    4.     public void DoSomething() { }
    5. }
    6.  
    7. public class ErrorExample : MonoBehaviour
    8. {
    9.     private void Awake()
    10.     {
    11.         Sample sample = null; // no actual object
    12.         System.Action action = sample.DoSomething;
    13.     }
    14. }
    Note that "DoSomething" is not being called, hence no null reference exception, but instead such error that you've encountered.

    Since you use "this.myClass1...." it's likely your field "myClass1" that hasn't been assigned and points to null when you want to fill the dictionary.

    *edit Corrected the variable's name.
     
    Last edited: Oct 7, 2019
    qcw27710 likes this.
  22. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    Thank you, turns out that when it was in
    Awake()
    , some variables weren't initialized (I guess), then I moved the assignment part to
    Start()
    , now it works without an error.
     
  23. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Guess? Easy to check.
     
    Darkwing4 and Joe-Censored like this.
  24. RobinHut3

    RobinHut3

    Joined:
    Apr 6, 2022
    Posts:
    6
    I dont think this was a waste of Time, I very much enjoyed this thead to the fullest, very funny.
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,701
    As funny as you find it, please don't necro threads like this.