x


ThreadCheck error when making Prefabs

I have this code for making an AI randomly every set amount of time. So every 5 seconds it makes a random prefab. Every 40 seconds it allows one more prefab to be made. I am doing this because I am making a Store sim and I want costumers be created and enter the store and the number of costumers allowed in the store slowly increase making the game harder over time.

The error Im getting only happens when I run the game in Unity3D and the moment the 5 seconds are up and its time to make a Prefab this error fills the Log and crashes unity.

m_ThreadCheck && !Thread::EqualsCurrentThreadID(m_ThreadID)

if I run the game as a EXE it runs fine making the AI every 5 seconds like it should. But after some time (anywhere between 20 seconds and 3 minutes of running the game) it crashes giving me an error saying to many threads. I dont code in c# so im not sure how my code is making a thread to begin with and why it is giving me that error. Here is the code:

private void creatAITimerevent(object source, ElapsedEventArgs e){
    if(Time.timeScale != 0.0f){
        counter ++;
        CapIncrease++;

        if(counter == timer){
            if(howManyAImade < howManyAIatOneTime){
                num = Random.Range(1,10);
                guyOutfit = (Random.Range(1, GuySet.Length)) - 1;
                girlOutfit = (Random.Range(1, GirlSet.Length)) - 1;

                if(num < 5){
                    Guy = (GameObject)Instantiate(GuySet[guyOutfit]);                       
                    Guy.name = SetNewName();        
                    GlobalObjectVariable.male++;
                    howManyAImade++;
                }else if(num > 4 && num < 9){
                    Girl = (GameObject)Instantiate(GirlSet[girlOutfit]);
                    Girl.name = SetNewName();
                    GlobalObjectVariable.female++;
                    howManyAImade++;
                }else if(num == 9){
                    BigGuy = (GameObject)Instantiate(BigGuySet);
                    BigGuy.name = SetNewName();
                    GlobalObjectVariable.bigGuy++;
                    howManyAImade++;
                }                   
            }
            counter = 0; //resets counter to start over
        }

        if(CapIncrease >= timeTellCapIncrease && (howManyAIatOneTime < howManyMaxAI)){
            howManyAIatOneTime++;
            CapIncrease = 0;
        }
    }
}

another thing is before I had it running in the Update() and that works never gives me a thread error or crashes the only problem with it is that I dont know how to make it create a AI every X amount of time and make it frame independent. The way it is written now if I have it in the Update it will make a AI ever seconds.

So my question is:

  1. how can I fix the code so that I can get it to not give me a thread error
  2. why is it giving me that error in the first place
  3. if I cant get it to work with a timer like it is not how do I make it work in the update() and make it frame independent
more ▼

asked Mar 21 '10 at 11:08 PM

Peter Paulsen gravatar image

Peter Paulsen
2 2 2 2

just an update. i put my code back to the Update() and now its still giving me that error. i don't really get why it is giving me a m_ThreadCheck && !Thread::EqualsCurrentThreadID(m_ThreadID) error when i make the prefab. i have narrowed it down to the fact that it happens the moment the prefab is generated.

Mar 22 '10 at 12:15 AM Peter Paulsen

Update 2.0:

i found that it was making the error because of some timers that were part of the AI (dont know why but thats another thing im going to work on) but im getting a new error

Thread::GetCurrentThreadID() != m_DeallocThread

its using the same code as above but thats the error that shows up when it makes the prefab

Mar 22 '10 at 12:36 AM Peter Paulsen
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

so anyways after a lot of testing i found out that using a timer is what makes that error. dont ask why i have no idea. but when i put it in the Update() it works only problum is the frame rate changed depending on the computer and the game itself. so i put it all in a

 FixedUpdate();

so it looks like:

void FixedUpdate(){
    if(!doTest){            
        if(Time.timeScale != 0.0f){
            counter ++;
            CapIncrease++;

            if(counter == timer){
                if(howManyAImade < howManyAIatOneTime){
                    num = Random.Range(1,10);
                    guyOutfit = (Random.Range(1, GuySet.Length)) - 1;
                    girlOutfit = (Random.Range(1, GirlSet.Length)) - 1;

                    if(num < 5){
                        Guy = (GameObject)Instantiate(GuySet[guyOutfit]);
                        Guy.name = SetNewName();        
                        GlobalObjectVariable.male++;
                        howManyAImade++;
                    }else if(num > 4 && num < 9){
                        Girl = (GameObject)Instantiate(GirlSet[girlOutfit]);
                        Girl.name = SetNewName();
                        GlobalObjectVariable.female++;
                        howManyAImade++;
                    }else if(num == 9){
                        BigGuy = (GameObject)Instantiate(BigGuySet);
                        BigGuy.name = SetNewName();
                        GlobalObjectVariable.bigGuy++;
                        howManyAImade++;
                    }
                }
                counter = 0; //resets counter to start over
            }

            if(CapIncrease >= timeTellCapIncrease && (howManyAIatOneTime < howManyMaxAI)){
                howManyAIatOneTime++;
                CapIncrease = 0;
            }
        }
    }
}

and i just make the timer however many seconds it will be * 60 because i have it running 60 frames per seconds (at lest thats what it has come out to be)

more ▼

answered Mar 22 '10 at 02:35 AM

Peter Paulsen gravatar image

Peter Paulsen
2 2 2 2

for future reference: Timers (in the System.Timers sense) use threads behind the scenes, so you'll generally get threading problems if you use them with unity functions. I'd have used a Coroutine for this, that way you can use WaitForSeconds between each call

May 31 '10 at 08:14 AM Mike 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x1951
x1257

asked: Mar 21 '10 at 11:08 PM

Seen: 1312 times

Last Updated: Jun 28 '10 at 07:35 AM