Forum rules - please read before posting.

NullReferenceException thrown on Menu.turnOn()

Hi,

I'm trying to turn on a Menu "My Menu" by script after clicking on a hotspot. This is my code:

void showMenu (Hotspot _hotspot, AC.Button _button){
Debug.Log ("Hotspot clicked : " + _hotspot);
Menu myMenu = AC.PlayerMenus.GetMenuWithName ("My Menu");

myMenu.TurnOn ();  <- this line throws the error
}
private void OnEnable(){
EventManager.OnHotspotInteract += showMenu;
}
private void OnDisable(){
EventManager.OnHotspotInteract -= showMenu;
}


When I run the code the following exception is thrown:

NullReferenceException: Object reference not set to an instance of an object
AC.PlayerInteraction+<UseObject>c__IteratorA.MoveNext () (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:1072

this is my Menu object:
image
image

when I make a call to an empty menu without any buttons (or other elements) there is no error.

is it a bug or is there anything I'm doing wrong?
thanks!

Comments

  • edited September 2016
    You'll have to check the order in which your scripts are run in unity. You'll need to wait until AC is done getting ready. If you try to call it's methods and objects before they are initialized or instantiated you'll always get nullreferences... You can also use Start() which is mostly safe in that case. Awake and OnEnable on the other hand occur too early... another approach would be to make a coroutine with a 0.1f/0.2f or so delay (which is usually enough for AC and other assets you may have) and call that in OnEnable or Awake.
  • ok thank's, adding a wait 0.2f coroutine solved it


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.