Forum rules - please read before posting.

Monobehaviour in Action Script

edited April 2019 in Technical Q&A

Hey,

we're trying to build an analytics script to post replies from a questionnaire we built as a game with Adventure creator.

Apologies, I'm not a coder but have someone who is actually quite good in coding but couldn't work out how to get monobehaviour working as AC Action.

We get the following error:
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at /Users/builduser/buildslave/unity/build/Runtime/Export/MonoBehaviour.bindings.cs:87)
AC.ActionQ1.Run () (at Assets/AdventureCreator/Scripts/Actions/ActionQ1.cs:118)

We did get it working as a custom script in menus but this limits the ability to have follow up actions after a button press. that's why we hoped to get this working as an AC Action.

We ran into a problem that we'd need monobehaviour class to post the result via yield return www. SendWebRequest

**That's what we try to use: **

private IEnumerator SaveData()
    {

                Debug.Log("Start of Coroutine");


        WWWForm form = new WWWForm();
        form.AddField("name", "XXX");
        form.AddField("q1", parameterStrings[0]);
        UnityWebRequest www = UnityWebRequest.Post("http://WEBLINK.php", form);
        yield return www.SendWebRequest();
        if (www.downloadHandler.text == "0")
        {
            Debug.Log("Section data saved");

                }
        else
        {
            Debug.Log("Save Failed. Error #" + www.downloadHandler.text);
        }
    }//end of savedata

**We tried to build it in as monobehaviour class: **

namespace AC
    {
public class MyClass : MonoBehaviour
{
        void Start()
        {

        }
}

LATER IN THE SCRIPT

        MyClass mc = new MyClass();
        mc.StartCoroutine(SaveData());


        if (!isRunning)
        {
            isRunning = true;
            return 0f;
        }
        else
        {
            isRunning = false;
            return 0f;
        }

I hope I was able to make it understandable.

_We just need to send results from menu buttons as string via SendWebRequest to our DB. We couldn't work it out as AC Actions seem not to like monobehaviour. _

I'm happy to post the full scrirpt but it has 320 lines. Might be a bit long for in here.

Any to have an idea?

Greetings.

Comments

  • edited April 2019

    Actions and MonoBehaviours are totally separate - applying the principles in the latter won't apply to the former.

    You can, however, call MonoBehaviour functions from within ActionLists - no need to create a custom Action. The Object: Call event and Object: Send message Actions can be used to run Unity component methods.

    It's also possible to hook into the OnMenuElementClick custom event to script the behaviour of a button click without ActionLists. Set the Button's Click type to Custom Script, and then place code akin to the following on a new C# MB script in your scene:

    using UnityEngine;
    using AC;
    
    public class MenuEventTest : MonoBehaviour
    {
    
        private void OnEnable ()
        {
            EventManager.OnMenuElementClick += My_OnClick;
        }
    
        private void OnDisable ()
        {
            EventManager.OnMenuElementClick -= My_OnClick;
        }
    
        private void My_OnClick (Menu menu, MenuElement element, int slot, int mouseButton)
        {
            Debug.Log ("Button " + element.title + " inside Menu " + menu.title + " was clicked");
        }
    
    }
    

    Where you can then check the titles of menu/element to determine if the correct Button was clicked.

    For more on menu scripting and custom events, see the Manual chapters of the same names.

  • Hey Chris, awesome. We'll give it a try. Thank you very much.

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.