Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug ArgumentException: Value does not fall within the expected range with delegates and events

Discussion in 'Scripting' started by Anima879, Sep 10, 2020.

  1. Anima879

    Anima879

    Joined:
    Jul 13, 2020
    Posts:
    10
    Hi everyone,

    I am new in Unity and I am trying to create a little chess game in 3D. Everything works fine until I add event.
    I have mutliple scripts :
    ChessGame : this is where the game run
    GameLogic : this script makes connection between ohter scripts
    ScoresUI : display eaten piece on the screen to keep track of piece who has been taken by each side.
    and ohter scripts.

    In ChessGame I have something like that :
    Code (CSharp):
    1. public delegate void PieceEatenCallback(Piece piece);
    2.  
    3.         public event PieceEatenCallback OnPieceEaten;
    4.        
    5.         void Eat(Piece eatenPiece)
    6.         {
    7.             _board[eatenPiece.Coord.GetXIndex(), eatenPiece.Coord.GetYIndex()] = null;
    8.             eatenPiece.IsAlive = false;
    9.             EatenPieces.Add(eatenPiece);
    10.             OnPieceEaten?.Invoke(eatenPiece);
    11.         }
    In the Start method of GameLogic, I subscribe the method :
    Code (CSharp):
    1. _chess.OnPieceEaten += ScoresUi.AddEatenPiece;
    And in ScoresUI :
    Code (CSharp):
    1. public void AddPiece(Piece.PieceType type, Player.Side side)
    2.         {
    3.             GameObject obj = new GameObject();
    4.             Image icon = obj.AddComponent<Image>();
    5.             icon.sprite = GetSprite(type, side);
    6.             RectTransform rt = obj.GetComponent<RectTransform>();
    7.             rt.sizeDelta = new Vector2(IconSize, IconSize);
    8.  
    9.             rt.SetParent(side.Equals(Player.Side.Black) ? WhitesPanel.transform : BlacksPanel.transform);
    10.  
    11.             obj.SetActive(true);
    12.         }
    13.  
    14.         public void AddEatenPiece(Piece piece)
    15.         {
    16.             AddPiece(piece.Type, piece.Side);
    17.         }
    But when I try to run this code, I have this exception :
    ArgumentException: Value does not fall within the expected range.
    Assets.Scripts.GameLogic.Start () (at Assets/Scripts/GameLogic.cs:54)
    The 54th line in GameLogic is the line where i subscribe the method.

    Do you have an idea of what's happening here and how I can solve this ?
    Thank you.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Debug.Log() is your friend here.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Code (CSharp):
    1. _chess.OnPieceEaten += ScoresUi.AddEatenPiece;
    I don't think you can just add a nonstatic method as an event subscriber without an object reference to call that method on. How would it know which instance of ScoresUi to call the method on? You need something like:
    Code (CSharp):
    1. ScoresUi myScoreInstance = <get this reference somehow>;
    2. _chess.OnPieceEaten += myScoreInstance.AddEatenPiece;
     
  4. Anima879

    Anima879

    Joined:
    Jul 13, 2020
    Posts:
    10
    Does ArgumentOutOfRangeException is equal to ArgumentException ?
     
  5. Anima879

    Anima879

    Joined:
    Jul 13, 2020
    Posts:
    10
    Sorry I should have post the declaration :
    Code (CSharp):
    1. public ScoresUI ScoresUi;
    I know it's confusing but even if I rename the variables, it doesn't fix the issue.
     
  6. Anima879

    Anima879

    Joined:
    Jul 13, 2020
    Posts:
    10
    I fixed my issue.
    The problem was I didn't put my ScoreUI object in my GameLogic object in the inspector :
    upload_2020-9-11_17-14-37.png
    I was expecting a NullReferenceException for that kind of problem.