Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Will_Croxford · May 24, 2018 at 10:24 AM · inputfielddelegatesevent-handlingargumentoutofrangeexceptionstatic variables

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 ArgumentExceptionerror:

 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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · May 24, 2018 at 10:28 AM 0
Share

Are you sure:

  • question.$$anonymous$$arySuccess is a function?

  • question.$$anonymous$$arySuccess 's signature is void $$anonymous$$arySuccess()?

avatar image Will_Croxford · May 24, 2018 at 11:43 AM 0
Share

Hi, yes public void $$anonymous$$arySuccess()is function in the Questions class thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IEnumerable called within a EventHandler. Can this be done? 1 Answer

Why is checking button click in Unity done this way? 1 Answer

How do I 'use an item'? 1 Answer

Why does my code hang when calling a script in a multi-nested event function 0 Answers

GetComponent() vs Delegate/Event 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges