Forum rules - please read before posting.

NullReferenceException when trying to use AC in a script

2

Comments

  • Is the UI Text component linked to AC's Menu Manager?  There may be a conflict if so, as AC will be trying to update the text box contents as well.

    Is this UI instantiated at runtime?  If so, the problem may be that it's not in the scene at the time that the find code is run.

    The name you're trying to find is very complex - try naming it something simple like "PortraitGridLabel" and call:

    GameObject go = GameObject.Find ("PortraitGridLabel");
    KeysGridName = go.GetComponent <Text>();

    Right before you assign the value of the text field - not in Awake, Start or anything.
  • No, the UI text is just UGUI, and not connected to AC's menu manager.

    The UI is being loaded at runtime, yes. It's always in the "DontDestroyOnLoad" part of the scene.
    Is there a way to have that UI load after everything else?

    I tried your two code snippets there, but i get the same issue still.
  • What do you mean "I can't simply link an AC var to the UI, since i also need to change graphics, and i can only do that via script"? Just as there are Get functions to read AC global variables there are Set functions to set the contents of the variables via scripting. I've setup a whole automatic alerts system for one of my projects which is pretty much done through scripting alone. Using AC Gvars for the text, I have the advantage of minimizing the amount of code necessary for displaying the text in the UI. As for the images, when you are using UGUI connected to AC, all scripting will still work as expected. Even if it's connected to AC, UGUI is UGUI. So you can easily still modify and control an UGUI instantiated by AC just as any other UGUI menu. 

    I mentioned the use of AC's Global variables for labels in UI because it's incredibly easy to implement, and you can do it anywhere, there's no need to place the script in the menu element at all when you are just changing the content of an AC global variable, not to mention AC will store the values for you when the player saves, and that's a whole load of work already done for you. 

    But anyway, my point is that, just because you are using UGUI connected to AC, doesn't mean you can't change the graphics in the elements using Unity's own GUI classes and functions. AC's functions are more like a shortcut when they are available. You can still drop scripts on the canvas or the menu elements for more direct control. And if you feel like one of the elements in you UI doesn't need to be connected, you can go ahead and remove it from the AC menu manager. In most cases all elements inside the canvas will still be visible when AC instantiates the UGUI, even if the elements don't exist in the menu manager.
  • edited November 2016
    Ah, didn't see you other post until now. Anyway, have you tried simply putting the script directly on the canvas? that way you'll know for sure that the script will work when the menu has already initialized (since the script will only work itself when the menu becomes enabled). That way you won't have to be guessing for the moment when the menu becomes available, to get a reference.
  • I tried that just now, and there not even the debug message shows, which DOES show when i just attach it to an empty object in the scene.
  • edited November 2016
    Can you show us the whole script again? (the other link is dead already). or show us a repro/minimal project? It's kinda hard to know what's really going on when we can't see the code or exactly how you are using the script too...
  • And what is the error message, exactly?
  • NullReferenceException: Object reference not set to an instance of an object
    WeMetKeysNow.OnMenuTurnOn (AC.Menu _menu, Boolean isInstant) (at Assets/WeMetKeysNow.cs:33)
  • The error means that Unity cannot find a Text component on any object named "LBL_LB1_UI_NB_Dossier_PortraitGrid_5".  Either the gameobject cannot be found, or there is no Text component on it.  Could it be that there are two gameobjects with the same name?
  • edited November 2016
    No, only one object with that name, and it has a text component..

    Update: I set it to "public" now and fed it the object in the editor itself and moved the label out of the grid it was in. Now it DOES set it, however it does not set it when the UI opens. I have to stop and re-start the game in order for the label to change.
  • Don't assign the UI prefab field directly - you're causing the prefab to change once another copy has already been loaded into the game.  This is why the change is not appearing until the game is restarted.

    Move it back to private and try assigning it at the time you need it, not in Awake / Start etc.  Otherwise, please post code and the full error message - it's impossible to advise on buggy scripts otherwise.
  • Code: http://paste.ofcode.org/SwY7TsxAa4VLnesBqbSMxq
    Full error:

    NullReferenceException: Object reference not set to an instance of an object
    WeMetKeysNow.OnMenuTurnOn (AC.Menu _menu, Boolean isInstant) (at Assets/WeMetKeysNow.cs:32)
    AC.EventManager.Call_OnMenuTurnOn (AC.Menu _menu, Boolean isInstant) (at Assets/AdventureCreator/Scripts/Managers/EventManager.cs:257)
    AC.Menu.TurnOn (Boolean doFade) (at Assets/AdventureCreator/Scripts/Menu/Menu classes/Menu.cs:1480)
    AC.ActionMenuState.Run () (at Assets/AdventureCreator/Scripts/Actions/ActionMenuState.cs:77)
    AC.ActionList+<RunAction>c__Iterator2.MoveNext () (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:329)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:293)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:485)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:398)
    AC.<RunAction>c__Iterator2:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:367)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:293)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:485)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:398)
    AC.<RunAction>c__Iterator2:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:367)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:293)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:480)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:398)
    AC.<RunAction>c__Iterator2:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:367)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:293)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:480)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:398)
    AC.<RunAction>c__Iterator2:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:367)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:293)
    AC.ActionList:BeginActionList(Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:261)
    AC.ActionList:Interact(Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:172)
    AC.ActionList:Interact() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:151)
    AC.<UseObject>c__IteratorA:MoveNext() (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:1121)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.PlayerInteraction:ClickButton(InteractionType, Int32, Int32, Hotspot) (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:816)
    AC.PlayerInteraction:HandleInteraction() (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:578)
    AC.PlayerInteraction:ContextSensitiveClick() (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:481)
    AC.PlayerInteraction:HandleInteractionMenu() (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:141)
    AC.PlayerInteraction:UpdateInteraction() (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:82)
    AC.StateHandler:Update() (at Assets/AdventureCreator/Scripts/Game engine/StateHandler.cs:313)
  • All that can be gleaned from that is what I said on the bottom of the first page - that either "LBL_LB1_UI_NB_Dossier_PortraitGrid_5" can't be found, or it can be found but has no Text component.

    This update will give you an error message telling you which one it is.  If there are multiple GameObjects that share the same name, it may be that Unity is finding the wrong one.
  • edited November 2016
    I was just wondering.. Does it matter if the UI prefab is not in the scene from the beginning? I tried running that same script with the UI prefab being open in the scene from the beginning, and it did not crash at least.. I would attach the script to the UI prefab, but as said, it doesn't even execute it then

    Update: Tried the new script, and indeed it cannot find the Label object..
    Cannot find gameobject named LBL_LB1_UI_NB_Dossier_PortraitGrid_5
  • It could be because the UI root Canvas object is disabled at the time, which causes Unity to overlook it.

    If you've linked it to AC's Menu Manager with "Unity Ui Prefab" as the Source, then yes - you'll need them to be instantiated at runtime.  If you set them to "Unity Ui In Scene", then they are expected to be present in the scene file itself.
  • The menu is linked as an Unity UI Prefab.

    Hmm, that is troubling. What would be my best option there? Is there any way of telling unity to find the object no matter if it's active or inactive? Since it does exist in the scene, just disabled
  • I shall change the ordering of the event call so that the event is triggered after the UI is enabled by AC.

    However, it's also occured to me that you're not checking the title of the Menu that's being turned on, meaning your code will run for every Menu that's being turned on.

    You will also have to begin your OnMenuTurnOn function with the following, replacing "MenuTitle" with the title of the Menu as written in the Menu Manager:

    if (_menu.title != "MenuTitle") return;
  • That update would be really appreciated, thanks :)

    I now start the function like this:







    if (WeMetKeys == true && _menu.title == "NotebookDossier" ), so this should be fine now.

    Would "if (_menu.title != "MenuTitle") return;" do the same, or is that something different?

  • So long as you place the rest of the function inside the "if" statement, that should be fine.
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.