Search Unity

Editor and GUI inconstant code

Discussion in 'Scripting' started by soft_sound, Apr 1, 2015.

  1. soft_sound

    soft_sound

    Joined:
    Aug 11, 2012
    Posts:
    39
    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!

    Code (CSharp):
    1. for (int i = 0; i < listLength; i++)
    2.             {
    3.                 Debug.Log("Run amount: " + i);
    4.                 Vector3 myPosition = Vector3.zero;
    5.  
    6.                 myPosition = placeObjects(i, spawnStyle, objectSetMargin, 0,1,2);
    7.  
    8.                 object createdObj = null;
    9.  
    10.  //Reset element amount
    11.                 if (currentElement < myObjs.Length)
    12.                     createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
    13.                 else
    14.                 {
    15.                     currentElement = 0;
    16.                     createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
    17.                 }
    18.  
    19.                 currentElement += 1;
    20.  
    21.                 GameObject setObj = null;
    22.  
    23.                 if(createdObj!=null)
    24.                     setObj = (GameObject)createdObj as GameObject;
    25. }
    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....
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  3. soft_sound

    soft_sound

    Joined:
    Aug 11, 2012
    Posts:
    39
    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. :/ (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....
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  5. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    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?
     
  6. soft_sound

    soft_sound

    Joined:
    Aug 11, 2012
    Posts:
    39
    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...


    Code (CSharp):
    1. //Reset element amount
    2.                 if (currentElement < myObjs.Length)
    3.                     createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
    4.                 else
    5.                 {
    6.                     currentElement = 0;
    7.                     createdObj = PrefabUtility.InstantiatePrefab(myObjs[currentElement]);
    8.                 }
    9.                 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!