Editor and GUI inconstant code

I’m running into a problem where code seems to run very inconstantly. I’m wondering if making my function static might fix that problem or if maybe using coroutines will help, but I’m not sure. I’m basically calling some code in OnGUI(), when I click a button that calls a function from another script object. This function seems to have maybe some sort of timing issue? I’m not sure.

Anyway, the code spawns objects by running through a loop. Looping seems to sorta work, but the spawning seems very random… It will never spawn the correct amount of object. Sometimes it might spawn 2 objects if I’m lucky other times it seems to spawn the 1st element multiple times… Any yes, I know what elements it should be spawning, if I fill my myObjs array with 4 objects, it should not be spawning the first element 4 times!

for (int i = 0; i < listLength; i++)
            {
                Debug.Log("Run amount: " + i);
                Vector3 myPosition = Vector3.zero;

                myPosition = placeObjects(i, spawnStyle, objectSetMargin, 0,1,2);

                object createdObj = null;

 //Reset element amount 
                if (currentElement < myObjs.Length)
                    createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
                else
                {
                    currentElement = 0;
                    createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
                }

                currentElement += 1;

                GameObject setObj = null;

                if(createdObj!=null)
                    setObj = (GameObject)createdObj as GameObject;
}

It will output that it ran through the loop x times correctly, but it will just skip spawning after the 0th element most of the time… And I’m not sure what I’m doing wrong…

createdObj turns null A LOT!

So I covered up the errors with a check, but I need to figure why it is not spawning…

OnGUI() runs in a separate thread, as far as I know, so you may be running into some concurrency issues. Consider setting a boolean (say spawnObjects = true) and in your next Update(), check if spawnObjects is true and perform your creation there?

Just a guess.

Oh, but I don’t use any Update() functions… All of my code is run in OnGUI or in other created objects (not GameObjects per say)… This code is run when the game IS NOT running (Aka editor mode…) so I don’t think any sort of update/start/awake functions would have any impact on my code. :confused: (Besides, I’m not using any of them…I don’t need them right now.) I think you are right about the concurrency issue, but I’m not sure how to check on that…

All code in Unity runs in the same thread. You have to explicitly use System.Threading to get threads, but none of the Unity API can be used there.

–Eric

Where is myObjs coming from? Is it possible that currentElement is never less than myObjs.length, so you’re always trying to instantiate the 0th element?

Hmmm, this code worked fine in my original class… I just split it up into another class so my class wasn’t so long… so I would not think the 0th element would be the problem…

csharp** **//Reset element amount if (currentElement < myObjs.Length) createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]); else { currentElement = 0; createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]); } currentElement += 1;** **

This code is extra…

I could totally comment it out and just put

createdObj = PrefabUtility.InstantiatePrefab(myObjs[0]);

or

createdObj = PrefabUtility.InstantiatePrefab(myObjs[1]);

And it would run the code correctly…

But now that I have been playing around with it, myObjs.Length does seem to be giving me trouble… I’m not sure why…If I set it to myObjs.Length-1, I get null errors everywhere…and the code still runs through x amount of times… But setting it to like 2 works fine. I guess the function is giving me errors.

Is there another function to get the length of a one-dimensional array…? I suppose I could just write one up myself, but I’m curious why this function causes null references…

…Thinking…

Nevermind, I figured it out, I don’t want to use the array length, as I made the array extra large and only use parts of it to spawn objects from. >.< Silly me, silly me, that is what happens when you come back to coding from a bit of a break… Now to figure out how to spawn objects right next to each despite size differences…

Thank you all for your help!