- Home /
Delegate value does not fall within the expected range
Hi, I am trying to pass an arbitrary function to be called inside a fixed function, which itself is called using delegate
keyword through the AddListener
function of InputField.onEndEdit
event. I am new to delegates, but as I understand, I need to create a new delegate to do this, and I'm getting bit tangled up, having read a few delegate articles still stuck. Currently, code compiles but get ArgumentException
error:
Value does not fall within the expected range.
CharacterController3D..ctor ()
Where the script name is CharacterController3D.cs - I don't know what ctor
means or if it is relevant. Line generating this error is: Del marySuccess = question.MarySuccess;
End Objective: When player inputs an answer to an arbitrary question posed when player triggers it in game, the ProcessAnswer
function will call a customised function processing correct answer to each question, ie each question will have different function dealing with a correct answer response. Likewise, the ProcessAnswer function will call different custom function for that question if player gives wrong answer and so on.
Below is the ProcessAnswer function, with arbitrary delegate passed to it delegate1
, which in following code block runs when correct answer is given:
private void ProcessAnswer (InputField input, string answer, float clockStart, Del delegate1)
{
if (String.Equals(answer, input.text.Trim(), StringComparison.OrdinalIgnoreCase)) // trim trailing spaces, accept any case in letters typed
{
countdownTimer.enabled = false; // stops timer
float timePoints = Mathf.Round(2000/(clockStart - countdownTimer.TimeStoppedTenthsSecond()));
delegate1();
}
else ....
Above function is called in same script by:
string answer = "grace";
clockStart = 90f;
inputField.onEndEdit.AddListener(delegate { ProcessAnswer(inputField, answer, clockStart, marySuccess); });
where marySuccess
is the delegate parameter in this case, defined by:
private delegate void Del(); // declare a delegate function wrapper with no parameters and returns void.
Del marySuccess = question.MarySuccess;
Still in this same script, question
here is declared as a static instance of a separate component script on same object. I understood have to make it static for the delegate to reference it, didn't work as non-static anyway:
private static Questions question;
and initialised in the Awake method, all in same script:
question = this.GetComponent<Questions>();
Any suggestions very grateful, thanks! I saw @Bunny83 gave great answer to delegate question not sure how difficult this is or if I'm on wrong track anyway.
Are you sure:
question.$$anonymous$$arySuccess
is a function?question.$$anonymous$$arySuccess
's signature isvoid $$anonymous$$arySuccess()
?
Hi, yes public void $$anonymous$$arySuccess()
is function in the Questions
class thanks
Answer by Will_Croxford · Sep 17, 2018 at 08:35 PM
I fixed this long time ago now, but in case anyone ends up here, the problem was access scope of the delegate function. Should be:
public delegate void Del();
Del marySuccess = question.MarySuccess;
In the code above, I had:
private delegate void Del();
MarySuccess is a public method, not a private one, so 'value does not fall within expected range' error, as I understand, occurs because since MarySuccess is a public method, therefore the signature of delegate Del() must be public, ie the 'value' of the delegate data type was a private function not public, therefore 'not within the expected range'.
More generally, as this was calling the function in a different class, of course the function called needed to be public, therefore the delegate must be declared as public delegate
....
Any more experienced developers welcome correct if I got some detail wrong here...
Thanks @Hellium for the tips anyway, the problem was the signature!
Your answer
Follow this Question
Related Questions
IEnumerable called within a EventHandler. Can this be done? 1 Answer
How do you make an animation start from a location dynamically? 2 Answers
Unity 3d 4.6b New GUI system doesn't take touch when already a touch event is occurring in 3d scene 0 Answers
Need ICanvasRaycastFilter for physics raycasts 0 Answers
can you assign serializable delegates in the inspector? 3 Answers