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
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. =)
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 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.
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.
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?).
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.
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 (100);
Thanks everyone whom contributed to this problem.
-Moon