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.
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.
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.
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.
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.
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.
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?)
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?
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.
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.
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?
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.
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.
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.
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://discussions.unity.com/t/481379
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.
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.
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.
note… “context” means the surrounding lines of code.
public class SomeClass : MonoBehaviour
{
public Dictionary<string, int> myDict = new Dictionary<string, int>();
void Awake()
{
myDict["A"] = 5;
}
}
(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.