Event won't call

What I’ve done:
When you are in the menu you can select dark mode on or off.
If you turn dark mode on and click play, it will send you to a dark theme game scene.
If you turn it off and click play it will send you to a light theme game scene.
As soon as you enter the dark themed game scene a variable “wasSetToDark” will be set to “yes”
If you click back in that scene it will take you to the menu scene where in the start checks if the “wasSetToDark” is “yes” and if it is, it will activate the darkmode.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using System;
using UnityEngine.SceneManagement;

public class MenuScript : MonoBehaviour
{
    public GameObject CamPos;
    public event Action ChangeToLight;
    public event Action ChangeToDark;
    public string DarkOrLight = "Light";
    public static string wasSetToDark = "no";

    public void Start()
    {
        if (wasSetToDark == "yes")
        {
            CamPos.transform.position = new Vector3(-6, 0, -25);
            DarkOrLight = "Dark";
            if (ChangeToDark != null)
            { ChangeToDark(); }
        }
    }
    public void Play()
    {
        if (DarkOrLight == "Light")
            {
                SceneManager.LoadScene("GameSceneLight");
            }
        else
            {
                SceneManager.LoadScene("GameSceneDark");
            }
    }
    public void DarkMode()
    {
        if (DarkOrLight == "Light")
            {
                CamPos.transform.position = new Vector3(-6, 0, -25);
                DarkOrLight = "Dark";
                if (ChangeToDark != null)
                { ChangeToDark(); }
            }

        else
            {
                CamPos.transform.position = new Vector3(-6, 0, 0);
                DarkOrLight = "Light";
                if (ChangeToLight != null)
                { ChangeToLight(); }
            }
    }
}
public class wasSetToDarkSETTER : MonoBehaviour
{
    void Start()
    {
        MenuScript.wasSetToDark = "yes";
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEditor;
using System;

public class GameOver : MonoBehaviour
{
    MenuScript ThemeChanger;
    void Start()
    {
        ThemeChanger = FindObjectOfType<MenuScript>();
        EndMenu.SetActive(false);
    
    }
    public void BackToMenuFromGame ()
    {
        SceneManager.LoadScene("MenuScene");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;

public class TextThemeChanger : MonoBehaviour
{
    public GameObject PlayL;
    public GameObject OptionsL;
    public GameObject QuitL;
    public GameObject VolumeL;
    public GameObject DarkModeL;
    public GameObject BackL;
    public GameObject PlayD;
    public GameObject OptionsD;
    public GameObject QuitD;
    public GameObject VolumeD;
    public GameObject DarkModeD;
    public GameObject BackD;
    MenuScript lightset;
    MenuScript darkset;

    void Start()
    {
       lightset = FindObjectOfType<MenuScript>();
       lightset.ChangeToLight += SetLightTheme;

       darkset = FindObjectOfType<MenuScript>();
       darkset.ChangeToDark += SetDarkTheme;
    }
    void SetDarkTheme()
    {
        PlayD.SetActive(true);
        OptionsD.SetActive(true);
        QuitD.SetActive(true);
        VolumeD.SetActive(true);
        DarkModeD.SetActive(true);
        BackD.SetActive(true);
        PlayL.SetActive(false);
        OptionsL.SetActive(false);
        QuitL.SetActive(false);
        VolumeL.SetActive(false);
        DarkModeL.SetActive(false);
        BackL.SetActive(false);
    }
    void SetLightTheme()
    {
        PlayL.SetActive(true);
        OptionsL.SetActive(true);
        QuitL.SetActive(true);
        VolumeL.SetActive(true);
        DarkModeL.SetActive(true);
        BackL.SetActive(true);
        PlayD.SetActive(false);
        OptionsD.SetActive(false);
        QuitD.SetActive(false);
        VolumeD.SetActive(false);
        DarkModeD.SetActive(false);
        BackD.SetActive(false);
    }
}

My problem is:

    public void Start()
    {
        if (wasSetToDark == "yes")
        {
            CamPos.transform.position = new Vector3(-6, 0, -25);
            DarkOrLight = "Dark";
            if (ChangeToDark != null)
            { ChangeToDark(); }
        }
    }

Here,

            if (ChangeToDark != null)
            { ChangeToDark(); }

This wont work.
Maybe a video could help you understand the problem:
723zrb

Btw I just started coding so please explain clearly :slight_smile:

When asking for debugging assistance, you should state what your code currently does. “This wont work” is not a description of what it does. Does it put an error in the Unity console? Does it crash the game? Does it silently do nothing? Does it hunt down your bank account online and transfer all your money to Nigerian princes?

I notice you are using string-type variables where you would probably be better off using bool, or maybe an enum. But that’s probably not your fundamental problem.

My guess is that there’s something wrong with how you initialized the variables ChangeToLight and ChangeToDark. What values are you assigning to those, and where is the assignment happening?

ChangeToLight and ChangeToDark works just fine. If i press the button that is assigned to those methods it works. But when I click back to menu from the game scene, it does not.
When i call the event via the start function everything in that function works but the event.
I dont really understand your question with the assignment but this is what you mean I think.
In the TextThemeChanger where i have stored a lot of repetitive code so the main script looks better,
I have this

{
    MenuScript lightset;
    MenuScript darkset;

    void Start()
    {
       lightset = FindObjectOfType<MenuScript>();
       lightset.ChangeToLight += SetLightTheme;

       darkset = FindObjectOfType<MenuScript>();
       darkset.ChangeToDark += SetDarkTheme;
    }
    void SetDarkTheme()
    {
//stuff//
    }
    void SetLightTheme()
    {
//stuff//
    }
}

in the main script i have this

public void DarkMode()
    {
        if (DarkOrLight == "Light")
            {
                CamPos.transform.position = new Vector3(-6, 0, -25);
                DarkOrLight = "Dark";
                if (ChangeToDark != null)
                { ChangeToDark(); }
            }

        else
            {
                CamPos.transform.position = new Vector3(-6, 0, 0);
                DarkOrLight = "Light";
                if (ChangeToLight != null)
                { ChangeToLight(); }
            }
    }

which works fine, but when i call it from here (same script) it doesent work.

    public void Start()
    {
        if (wasSetToDark == "yes")
        {
            CamPos.transform.position = new Vector3(-6, 0, -25);
            DarkOrLight = "Dark";
            if (ChangeToDark != null)
            { ChangeToDark(); }
            wasSetToDark = "no";
        }
    }

wasSetToDark is automatically set to yes when you enter the dark theme version of the game
I made sure to test it’s still bugged because usually when i ask for help it magically fixes for some reason…
“Does it silently do nothing?” yep, only when called from the start function
Also not gonna lie the thing with the prince got me.

That sentence doesn’t make sense. ChangeToLight and ChangeToDark aren’t methods, they’re events. Buttons cannot be assigned to events, nor can events be assigned to buttons.

Your most recent post shows you assigning listeners to those events from inside the Start script of some other class. Have you changed your script execution order to guarantee that those two Start functions run in the correct order? If not, there is no guarantee that the listeners are being assigned before you try to invoke the events.

1 Like