Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question gameObject.SetActive (false); Not working

Discussion in 'Scripting' started by xx_Blackwolf99, Jan 28, 2021.

  1. xx_Blackwolf99

    xx_Blackwolf99

    Joined:
    Jul 7, 2019
    Posts:
    5
    I have a strange problem with the SetActive funktion.
    First i got a callback from an Event:


    Code (CSharp):
    1.  private void im_LoginOK(object sender, EventArgs e)
    2.         {
    3.             Debug.Log("Connected");
    4.             ChanceScreen(1);
    5.             try
    6.             {
    7.                 Debug.Log("Online User: " + im.Users().ToString());
    8.             }
    9.             catch (Exception ex)
    10.             {
    11.                 Debug.Log("Error: " + ex.Message);
    12.             }
    13.         }

    The "Connected" is showed correctly in the console.
    But the ChanceScreen will not executed.


    Code (CSharp):
    1. private void ChanceScreen(int Screen)
    2.         {
    3.             Debug.Log("2");
    4.             if (Screen == 0)
    5.             {
    6.                 LoginForm.SetActive(true);
    7.                 //MainForm.SetActive(false);
    8.                 MainForm.GetComponent<DisableScript>().DisableThis();
    9.                 Debug.Log("Chance Screen to Login");
    10.             }
    11.             else if (Screen == 1)
    12.             {
    13.                 Debug.Log("3");
    14.                 LoginForm.SetActive(false);
    15.                 Debug.Log("4");
    16.                 MainForm.SetActive(true);
    17.                 Debug.Log("5");
    18.                 Debug.Log("Chance Screen to Main");
    19.             }
    20.         }
    The last Console log is 2 from the Debug.Logs...
    The Forms i will deaktivate are gameobjects which are not on this script. They are at a completely different position in the scene.

    Anyone has an idea why this isnt working? ty
     
    Airmouse likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    I'm going to make a wild guess and say that the value of Screen is something else than 0 or 1. Try

    Code (csharp):
    1.  
    2. private void ChanceScreen(int Screen) {
    3.     Debug.Log("Screen is: " + Screen);
     
    Kurt-Dekker likes this.
  3. xx_Blackwolf99

    xx_Blackwolf99

    Joined:
    Jul 7, 2019
    Posts:
    5
    No, its not that simple :D
    The ChanceScreen Function was only implementet, to Outsource the functions.
    First i had the setActive(False) function in the Event callback and this also doesnt work.
    I debuggt it an the function interrupted at the setActive position. Idk




    The DisableScript is a Script on the MainForm to setActive(Fallse) itself...Annother try to get this.
    I also have to say that the ChanceScreen funktion is executes at the beginning of the programm with the value 0 and this works.
     
    Last edited: Jan 28, 2021
  4. xx_Blackwolf99

    xx_Blackwolf99

    Joined:
    Jul 7, 2019
    Posts:
    5
    Is it possible, that you cant run any Unity Funktions from a Eventcallback, that comes from a non MonoBehaviour class?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The information you gave in your second post contradicts information from your original post. Your original post said that you saw debug of "2" but not "3", but your screenshot of your console shows a "3". Your original post also had different code after the "3".

    These sorts of details are important when asking for debugging help!

    Most Unity functions can only be used from the main thread. So if your callback is invoked from a different thread, then yes, that would likely explain your problem.

    (I was going to suggest something like this when I first saw your thread, but ruled it out because it wouldn't explain why you got the debug message "2" but not "3"...)

    The standard solution for this is to have the callback set some variables showing what needs to happen, and then have a Unity-driven function (like Update) check for those variables and then actually make the changes.
     
  6. xx_Blackwolf99

    xx_Blackwolf99

    Joined:
    Jul 7, 2019
    Posts:
    5
    Yes, you are right. But the problem is, that im trying different possible solutions. So between my posts there are a lot of chances. Sure its not good, but i cant put all my hope into this post, im also trying for my own.

    But thank you. I already thought about this, otherwise there is no sense. Do you have an Idea how to bypass this?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Is this event callback happening in another thread (not the main thread?) Unity will not allow you to modify GameObjects etc.. from outside the main thread. Your thread will simply be unceremoniously killed.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It's great that you're trying things on your own and not pinning all your hopes on one Internet thread.

    But it's still important that the information you post when asking for help is accurate. Otherwise you are wasting your time--and the time of every generous person who is trying to help you. That's both foolish and very unkind.

    If you really can't spare enough time to get the details right in your thread, you shouldn't post a thread at all.

    Apparently you didn't make time to finish reading my previous reply, either.
     
    Kurt-Dekker likes this.
  9. xx_Blackwolf99

    xx_Blackwolf99

    Joined:
    Jul 7, 2019
    Posts:
    5
    Actually i did exactly this. I dont know why, but i thought there is a better resolution. But ty