Forum rules - please read before posting.

[SOLVED] Getting a menu to talk to a script

edited April 2014 in Technical Q&A
Hi,

I'm trying to build a menu that has a row of buttons and one big label on it. When I press one of the buttons I want the text in the label to change. So basically all I want is the click on a button to trigger a custom function that fills up the label with the right string. But nothing seems to happen. 

I wrote this function:

public void ShowNotes (int item) {
if (item == 0) {
PlayerMenus.GetElementWithName("Dossier", "Notes").title = "Bla bla bla bla";
}
}

When I click a button on the menu it sends a message to this function with an int (can't send strings to custom functions?) through a menuactionlist. But I get a null reference (int32) when I try to trigger this, and nothing happens. The script I'm trying to talk to is attached to a gameobject in the scene with a custom ID script to reference it.

I've tried stuff like this before to update the text on a button but I can't seem to get scripts to influence AC elements in any way. And there is no scripting reference to turn to so here I am.

I've also tried:

- putting a journal instead of a label, but I can't jump to a specific journal page
- making each label text its own menu and toggling those when pressing the buttons - yikes
- using items instead of buttons and having their use action trigger a journal update

Am I better off building a menu through script with UnityGUI?

Comments

  • edited April 2014
    GetElementWithName returns a MenuElement class, but the MenuLabel you want is a subclass, so you have to convert it.  You also want to affect it's label field, not it's title - which is used for display in the Menu Manager only.  The correct code would be:

    MenuLabel labelElement = (MenuLabel) PlayerMenus.GetElementWithName ("Dossier", "Notes");
    labelElement.label = "Bla";

    However, you do have a couple of other options:

    1) You display multiple Labels in the same place, but only one shown at a time.  The idea being that you switch display from one to another when you click the Button.  You can use the Menu: Change state Action to show and hide Elements in a Menu.

    2) Set the Button's Click type to Custom Script.  Then in MenuSystem.cs, you can type the code that you want to be executed in the OnElementClick function:

    if (_menu.title == "Dossier" && _element.title == "Notes")
    {
      MenuLabel labelElement = (MenuLabel) _menu.GetElementWithName (_element.title);
      labelElement.label = "Bla";
    }

  • edited April 2014
    Ah okay rad, I already thought it was a bit strange that I wasn't seeing a .label option in autocomplete, so I figured .title was what contained the text, and .name was what it was called in the inspector.

    Anyway it works like a charm! I wrote a kind of hybrid solution where I do use MenuSystem but have it pass along the necessary variables to another function in a custom script of mine.

    My initial reservation with editing MenuSystem.cs is that it is included in the AC folder, and thus very prone to being overwritten during an update. Is it safe to drag it to another folder? Then it's ideal, since there will be other elements on the GUI I want to trigger through script.

    I dig your suggestion for overlaying the labels too, that makes it nicely integrated with AC. But with the above method working fine I'd rather stick with coding (call me crazy!)
  • I think Unity looks for it's own internal ID when updating scripts, so moving it to another folder likely won't make a difference.  However, that script is "locked" so far as I'm concerned, and won't be altered by mself - so you should be able to just replace it with your own copy after an update.
  • edited April 2014
    Ok, good to know it probably won't change. I had moved it to another folder and everything still worked fine, and when updating to 1.30 it still recognized it was that script and offered to overwrite it even when it was outside of AC's folder structure (which ofcourse I deselected), but that confirms Unity does keep track of it as such.
  • @hedgefield, feel like sharing?
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.