# Some UI button script not working in apk build!

There are a lot of posts regarding this problem in various website and even in this one but no one has given a feasible solution to this problem. I have a button that has a custom sprite. I also have a custom script attached to that button which when pressed down (IPOINTER DOWN) changes the color of particular sprite renderer. All this works in pc but not on apk build. Why? I have two canvas, so I thought there might be raycasting issues, so I disabled one but still no luck. The buttons are in world space in canvas. So, I thought I might not use a canvas but IPointer only works on clickable UI elements.

The scene looks like this, star is the clickable button and the borders are a sprite(district).

The script attached to the button is:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.

public class OnHighlighted : MonoBehaviour, IPointerClickHandler, IPointerDownHandler// required interface when using the OnPointerEnter method.
{
// HoldByeach button

private GameObject correspondingImage;

void Start ()
{
correspondingImage = GameObject.Find(gameObject.name);
}

public void OnPointerClick(PointerEventData pointerEventData)
{
GetComponent<Image>().color = new Color32 (36,53,53,255);
correspondingImage.GetComponent<SpriteRenderer>().color= new Color32 (36,53,53,255);
}

public void OnPointerDown(PointerEventData pointerEventData)
{
GetComponent<Image>().color = new Color32 (47,255,0,255);
correspondingImage.GetComponent<SpriteRenderer>().color = new Color32 (47,255,0,255);
}
}

• Can you walk us through the steps to create a Minimal Complete Verifiable Example of this problem? Once we can reproduce the issue, we can test potential solutions to be sure they work. – DMGregory Jul 3 '20 at 12:22

In your Start() function you do this:

correspondingImage = GameObject.Find(gameObject.name);


First of all, GameObject.Find() is extremely sloppy and you should never use it. Second, this tells the script to find the GameObject with the same name as the GameObject that the script is attached to. This makes no sense, as it's likely to return the same GameObject, which you already have a reference to.

void Start() {
correspondingImage = GameObject.Find(gameObject.name);
print(correspondingImage == gameObject); //should print "true"
}


The exception is if you have multiple GameObjects in the scene with the same name; if you do, you definitely should not be trying to find them with GameObject.Find()! It's possible that the one that is found in the Editor is not the same as the one that is found in the Android build.

You should use direct references to your objects when possible:

[SerializeField] SpriteRenderer correspondingImage; //we can set this from the inspector

void Start() {
Assert.IsNotNull(correspondingImage);
}


This will improve performance, because Unity doesn't have to search the scene for a particular object, and also is less likely to break.

• Thanks man, I didn't realize the problem was the initialization of the corresponding image. But in this solution, you tell me to manually go and drag the corresponding image and that is not possible because there are seventy five corresponding image to drag. Are there any feasible solution to this? – Sabin Pyakurel Jul 4 '20 at 6:23
• I got this problem even in playmode in unity now. I think I found the problem. Whenever I clicked a button that was not working, it is throwing null reference exception. I don't know why? – Sabin Pyakurel Jul 4 '20 at 8:36
• Have you tried void OnValidate() { correspondingImage = GetComponent<SpriteRenderer>(); } to automatically populate these references at edit time? You can also include error logging here if correspondingImage == null after the assignment attempt, to help you track down which buttons are incorrectly configured. – DMGregory Jul 4 '20 at 15:26
• I got it fixed with both your and kevin's solution. – Sabin Pyakurel Jul 5 '20 at 4:38