Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting an odd error. "Cast thrown exception".

Discussion in 'Scripting' started by MoonBoy, Jan 7, 2015.

  1. MoonBoy

    MoonBoy

    Joined:
    Jan 7, 2015
    Posts:
    6
    Hi,
    I've been working with a script that places objects into a queue to be later instantiated, and it only finishes queuing when it goes over the allowed limit. The odd part is that the script malfunctions, yet doesn't prevent me from playing my game in Unity ("With the exception that my script completely malfunctions").

    When I go through debugging, it seems to cycle all the way through my update script, until it hits: if(objectQueue.Count < 100){ }. When it reaches this point, it seems to skip this, and return to the top of the script, as if ignoring the whole line of code. It gives off the warning error "Cannot cast thrown exception" when I haven't thrown or caught anything in the script, nor have I made and referenced something else above the script that the script couldn't identify.




    This is partition of the script that acts up.

    if (objectQueue.Count < 100) { <<<< This part gives off the error.
    RandomGen ();
    chosenInstantiate = myPrefabs[clone];
    objectQueue.Enqueue ((GameObject)(Instantiate (myPrefabs [clone])));
    }

    The script references I am using are:
    using UnityEngine;
    using System.Collections.Generic;
    using System.Linq;


    I'm clueless as to how to go about this, and why it's not working, so I'm hoping that someone knows a little about what's happening?

    Thanks for taking your time to read, and for help.
    -Moon
     
  2. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    What kind of variable is objectQueue?
    Also, please use code tags so we can more easily read the code you've posted. You'll likely get more responses this way,too. =)
     
    MoonBoy likes this.
  3. MoonBoy

    MoonBoy

    Joined:
    Jan 7, 2015
    Posts:
    6
    Hi! Sorry for not doing this before. objectQueue is a Queue that stores game objects.

    I'm not familiar with what tags are. I'm new with unity forums, so my apologies (and forums in general).

    private Queue<GameObject> objectQueue; is the definer of of the variable.
     
  4. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    I'm not terribly familiar with Queues I'm afraid, so I'll be of limited help. Hopefully someone more knowledgeable on the subject will poke their heads in here.
    As a semi-educated guess though, given that your code looks fine to me, have you tried defining it as
    Queue<GameObject> objectQueue = new Queue();

    The error your getting seems to be related to mixing variable types, so it doesn't recognize objectQueue as an int when you check if it's lower than the value you supplied.
    It is, however, 5am here, so I could be completely missing the mark.
     
    MoonBoy likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Puzzling, the code has not obvious errors. Here is a brain dump for you.

    Is this a warning or an error?

    Which line is it referring too exactly?

    The only casting is happening in your instantiate line. Try split this line into two, one to instantiate and one to enqueue.

    Debug the objectQueue.Count every frame. Doe this error occur the first time through, or does it occur only when the count reaches some number?
     
    MoonBoy likes this.
  6. MoonBoy

    MoonBoy

    Joined:
    Jan 7, 2015
    Posts:
    6
    I've went ahead beforehand and used Unity debugger, and found the error to be "cannot cast thrown exception". This script doesn't interfere with the script, yet it does however not function correctly. This error reaches the first time through, as soon as it reaches the .count part.

    It's a yellow warning (exclamation mark).

    I'll go ahead and take your advice, and play around with that for awhile.

    Thanks for the response =)
     
  7. MoonBoy

    MoonBoy

    Joined:
    Jan 7, 2015
    Posts:
    6
    I've looked around for my errors for quite awhile, and I could never find any cast thrown exception errors. Queue.Count counts the amount of objects in the queue, and interprets it as a number, so that's why I figured Queue.Count could work. I'll mess around with the script a little and see if that will work if I make it interpret a value that way. Maybe place a counter beside the enqueues and dequeues.
    Thanks for the support, and good morning (or since it's later, good afternoon/night?).
     
  8. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Have you tried using .Count elsewhere in your script, maybe with Debug.Log, to see if it also returns a warning for that? If it does, we know it's .Count causing it. If it doesn't, it must be something to do with the context in which you're using it.
    Either way, it would help narrow it down a little.
     
    MoonBoy likes this.
  9. MoonBoy

    MoonBoy

    Joined:
    Jan 7, 2015
    Posts:
    6
    I've found out the cause! It turns out that I haven't defined the amount of objects allowed. This was easily fixed by sticking the code snippet: objectQueue = new Queue<GameObject> (100);

    Thanks everyone whom contributed to this problem.
    -Moon
     
  10. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Ahh, that makes sense. I'm glad you found the issue. =)
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Odd. Glad it worked, but according to the docs you shouldn't have needed to do that.