Forum rules - please read before posting.

UI Script not working if AC loads it

Hi all,

I have a menu for giving NPCs gifts, it's in Adventure Creator's menu system but as a Unity UI Prefab.

It's an inventory, and all my buttons have the same base sprite.

I have a script on all the buttons that change their sprite if they were clicked, to a new sprite which looks exactly the same as the base sprite but with a border (so basically a border around the item when the button is clicked).

Then all the other buttons in the inventory get reverted to the base sprite so only the button selected has the border.

The issue is, it works if the menu was already enabled in the scene, however if AC controls it/turns it on when the gift button is pressed then the changing sprite part of the scripts do not work.

Doing a Debug.Log shows the script goes through but the sprite doesn't change.

The script is called with OnClick() in the Button (Script) component.

Here is the code: https://pastebin.com/HXPUFCTD

I hope this makes sense, I'm terrible at explaining things lol

Thank you for your help :)

Comments

  • Click the Inspector fields in the prefab instance that spawns in the scene to ping the objects that they reference - do they ping to the correct GameObjects, or the original prefab?

  • When I click the referenced image object it seems to ping the one in the game, if that's what you mean?

  • It is - this is the instance spawned in by AC, not the original you had in the scene beforehand?

    AC won't affect a Button's Image or override its click events - but it will set the Buttons Interactable state.

    If the script's Inspector has all of the Image fields assigned, it doesn't need to use GetComponent calls inside the functions.

    When testing with a Debug.Log, try something like this at the bottom of ChangeGraphic1:

    Debug.Log ("Clicked " + Image1 + ", Sprite: " + Image1.sprite, Image1);
    

    Click the log when it appears to ping the Image1 object - does it ping the right object, and what does the message say?

  • Yes it's spawned by AC, it's not in the scene beforehand.
    The text says:
    Clicked GiveGiftButton (1) (UnityEngine.UI.Image), Sprite: itemselect (UnityEngine.Sprite)
    Which helped me notice the button sprite was being used for the inventory item sprite... so I made a new button for the border and it all seems to work now. Dumb mistake. Thank you Chris for your time :)

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.