0
\$\begingroup\$

I have set up a script to set game objects as active/inactive depending on a particular number representing the index of a gameObject array. As you can see the image below. I have two buttons as shown on the page, a backward button and a forward button. I have attached event trigger components to each of the buttons so as to turn pages forward / backward depending on which button I click

Almanac

The forward button works but this is not the case with the backward button. The function is not even being called. In fact only this sprite which on being clicked does not call the function assigned to it.

More details on the backbutton : Details of the backbutton

Here is the script for the MonsterRosterManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterRosterManager : MonoBehaviour 
{
    int MonsterPageNo;
    public GameObject[] MonsterPages;

    void Start()
    {
        MonsterPageNo = 0;
    }

    void Update()
    {
        switch (MonsterPageNo)
        {
            case 0:
                MonsterPages[0].SetActive(true);
                MonsterPages[1].SetActive(false);
                MonsterPages[2].SetActive(false);
                break;
            case 1:
                MonsterPages[0].SetActive(false);
                MonsterPages[1].SetActive(true);
                MonsterPages[2].SetActive(false);
                break;
            case 2:
                MonsterPages[0].SetActive(false);
                MonsterPages[1].SetActive(false);
                MonsterPages[2].SetActive(true);
                break;
        }
    }

    public void PrevMonsterPage()
    {
        Debug.Log ("Monster Page No to decrement: "+MonsterPageNo);
        MonsterPageNo -= 1;
        if (MonsterPageNo == -1)
            MonsterPageNo = 0;
    }

    public void NextMonsterPage()
    {
        Debug.Log ("Monster Page No to increment: "+MonsterPageNo);
        MonsterPageNo += 1;
        if (MonsterPageNo == MonsterPages.Length)
            MonsterPageNo = MonsterPages.Length - 1;
    }
}

Furthermore, the logs below show that the NextMonsterPage() function is called on clicking the forward button, but no calls to the PrevMonsterPage() are being registered.

logs on clicking the forward button

What am I overlooking here?

\$\endgroup\$
  • \$\begingroup\$ There may be another UI element that is overlapping your back button, causing the clicks to be missed. Try moving your back button away from all elements and clicking it to check if that is the case. \$\endgroup\$ – Savlon May 5 '17 at 20:49
  • \$\begingroup\$ Also insure that the button belongs to a canvas with a raycast target? I forget what it's called but when you first create a UI object with a canvas, its attached on the same object as the canvas, but if you attach a canvas component manually it does not come with the raycast component automatically, you have to add that separately. \$\endgroup\$ – Draco18s May 5 '17 at 21:48
4
\$\begingroup\$

I see that the backward image is turned around 180 degrees to make it face opposite the direction of the forward image. This means that its front side is facing away from the camera. By default, this will cause the event system to ignore it. There are three easy fixes for this.

1. Make a copy of the arrow texture and use your image editor to reverse it. Use the copy instead of rotating the button.

2. Use an x scale of -1 instead of a y rotation of 180. This doesn't change the facing, so the image still gets events.

3. Find the Graphic Raycaster component (usually on same GameObject as the Canvas for your UI objects). Clear the checkbox labeled Ignore Reversed Graphics.

I'd probably go with 2, since it uses the same graphic you already have and it doesn't run the risk of messing anything up by doing something unusual with the user interaction components.

\$\endgroup\$
  • \$\begingroup\$ Oh, good eye! I hadn't noticed that. \$\endgroup\$ – Draco18s May 6 '17 at 19:10
  • \$\begingroup\$ @Victor T. : Thanks a lot mate. It's working well now. Appreciate the help. \$\endgroup\$ – Pavan Vasan May 7 '17 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.