Forum rules - please read before posting.

How to bind Toggle element to custom script?

edited September 2014 in Technical Q&A
hey there,

I wanted to make an extra toggle in the options menu for turning the Display Indefinite setting in the Speech Manager off and on, but I'm having some trouble figuring out how to do that.

In MenuSystem.cs I put:

if (_menu.title == "Options") {
if (_element.title == "ContinueToggle") {
if (speechManager.displayForever == false)
speechManager.displayForever = true;
else
speechManager.displayForever = false;
}
}

But it throws a nullref exception on the speechManager.displayForever line. I instantiated the SpeechManager above like this:

static private SpeechManager speechManager; and on start: speechManager = GameObject.FindObjectOfType(typeof(SpeechManager)) as SpeechManager;

How do I bind the On and Off state of a toggle succesfully to a custom script?

Comments

  • You need to reference the speechManager variable in the References prefab.  Since your project folder may contain multiple Speech Manager assets, which one is currently active is stored in References.  Use this:

    AdvGame.GetReferences ().speechManager;

    Incidentally, if you ever wanted to stop AC reverting to the Demo managers when upgrading, simply uncheck the import of the References asset!
  • Ahh right, yep swapping that for the FindObjectOfType thing did the trick + some extra code to get the toggle working right.

    For completeness sake, here is the updated code. I linked variable 5 to the Options data to keep the settings and the menu button state in sync:

    if (_menu.title == "Options") {
    if (_element.title == "ContinueToggle") {
    bool togglestate = GlobalVariables.GetBooleanValue(5);
    if (togglestate == true) {
    GlobalVariables.SetBooleanValue(5, false);
    speechManager.displayForever = true;
    }
    else {
    GlobalVariables.SetBooleanValue(5, true);
    speechManager.displayForever = false;
    }
    }
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.