0

I did a game but at the end I encountered a problem. I have two classes that perform certain transformations on the game.

This is the game

I want when I type on a round cell to tell me a Debug.Log that the number odd or even;

Below is the event created for each cell

    private void GetValue(GameObject target, MouseEventType type)
{
    if (type == MouseEventType.CLICK)
    {
       int targetIndex = System.Array.IndexOf(molecula.atom, target);
       if( molecula.atom[targetIndex].GetNumber() % 2 == 1)
        {
            Debug.Log("God Click");
        }
        else
        {
            Debug.Log("Bad Click");
        }
    }
}

Now that I type on a cell I get the error below enter image description here

If anyone can tell me what I'm wrong with?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
George
  • 45
  • 7
  • 5
    Use the [Debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/debugging-managed-code), Luke! – Uwe Keim Jul 14 '17 at 12:22
  • 7
    If the item cannot be found, `IndexOf` returns -1. That would explain the error in the next line. – brijber Jul 14 '17 at 12:23
  • which line is 41? Btw your title is not at all about the problem. Short answer is: `You are trying to read an index of an array that does not exist (the index)` – online Thomas Jul 14 '17 at 12:24
  • public Atom[] atom; molecule.atom – George Jul 14 '17 at 12:26
  • this is my array – George Jul 14 '17 at 12:26
  • Of the atom class type – George Jul 14 '17 at 12:27
  • Looking at molecula.atom[targetIndex], What is the length of molecula.atom and what is the value of targetIndex? I think that (targetIndex - 1) is larger than molecula.atom.Length – Joe_DM Jul 14 '17 at 12:27
  • `IndexOf` returns `-1` when no such value was found. For your case I would create a custom `IComparer` and use it as an additional parameter to find that particular index instead of handing all of the comparing logic to C# internals ( reference comparison ). – mrogal.ski Jul 14 '17 at 12:30
  • 1
    @Joe_DM `IndexOf` will either return an index in the array or -1 so I doubt the index is too large. – juharr Jul 14 '17 at 12:30
  • 1
    @m.rogalski I'm guessing the `target` should be a reference to an object in the array, in any case if two molecules have the same value you don't want to click on one and have it pick the other one. – juharr Jul 14 '17 at 12:31
  • The issue might not be inside of that code snippet. What does `GetNumber` do? – Joe_DM Jul 14 '17 at 12:32
  • @Joe_DM The error says it's happening in the `GetValue` method. – juharr Jul 14 '17 at 12:33
  • public int GetNumber() { nr = idx; string numberString = nr.ToString(); cifre = numberString.ToCharArray(); for(int i=0; i – George Jul 14 '17 at 12:33
  • @juharr good point. It certainly looks like this exception would happen if `target` doesn't exist inside of the array. Quick test: string[] arr = new string[] {"test", "foo"}; int targetIndex = System.Array.IndexOf(arr, "bar"); Console.WriteLine(arr[targetIndex]); result = Index was outside the bounds of the array. – Joe_DM Jul 14 '17 at 12:34
  • @juharr Theoretically you're correct, But what if he's just creating a new instance of some random object which is then used in that comparison? Result of that will be `-1` because the reference is not the same. That's my guess of what's wrong with this code. – mrogal.ski Jul 14 '17 at 12:35
  • 1
    @m.rogalski If it is then that's the problem. It should use the original reference. So, a good question for the OP is how is `GetValue` being called. I haven't used Unity but it looks like a event handler which should return the reference of the object that was clicked and one would hope that's the same as what's in the array. – juharr Jul 14 '17 at 12:36
  • @juharr I think you're on to something. What happens if you do a Debug.Log(targetIndex)? – Joe_DM Jul 14 '17 at 12:37
  • for (int i = 0; i < molecula.atom.Length; i++) { molecula.atom[i].gameObject.AddComponent().MouseEvent += GetValue; } – George Jul 14 '17 at 12:38
  • Ultimately you're going to have to debug your code to figure out the problem as there really isn't enough information here for us to do anything other than guess. – juharr Jul 14 '17 at 12:39
  • targetIndex is -1 – George Jul 14 '17 at 12:40
  • on Debug.Log(targetIndex) – George Jul 14 '17 at 12:40
  • I don't know unity well, However, Would it be easier to get the object reference by using a raycast from mouse to screen instead of searching the array? This would eliminate the need to look in the array and you could use the reference directly to get the number? – Joe_DM Jul 14 '17 at 12:41
  • @George That means `target` wasn't in the array. You need to determine why that is keeping in mind that you want the references to be the same. – juharr Jul 14 '17 at 12:42
  • Well, I'm gonna solve the problem, thanks a lot – George Jul 14 '17 at 12:42

0 Answers0