0

Right now in my Unity project I have a list of buttons that are spawned in a scroll list when I start my game. Each of these buttons are linked to a prefab button. I am trying to make it so that when I click any of these buttons, an image pops up. I have this code attached to my prefab button, and I tested it using a debug log, and that works fine, but the image will not actually pop up. I am not sure what the issue is. It is all dealing with my UI. Here is my code:

public Image popUpImage;

void Start () 
{
    popUpImage.enabled = false;
    button.onClick.AddListener (activatePopUp);
}

public void activatePopUp()
{
    popUpImage.enabled = true;
    Debug.Log("pop up is active");
}

Why won't my image actually pop up if the debug is working? I attached my image to the inspector as well.

coder5777
  • 23
  • 1
  • 7
  • The image you assigned is in the scene I assume? Generally, linking in prefabs (it's only a prefab when it is in the assets folder) can be problematic since in the game you will need references to objects in the scene (which might be instances of a prefab). – Gunnar B. May 02 '18 at 18:49
  • @GunnarB. yeah its all in the same scene. And yeah I'm realizing this now. Is there another way to go around this? – coder5777 May 02 '18 at 19:14
  • Well, if everything is in one prefab, the links should actually work properly when you instantiate it, not quite sure about that though. Otherwise, if you can't combine it, you will have to set up the references in code (if you do this on runtime). Otherwise you can set it up in the inspector off course, just need to make sure to do it for the actual instance(s) of the button(s), not the prefab. For more specific solutions you'd need to give some more information about what you have, e.g. with some screenshots. – Gunnar B. May 02 '18 at 21:41

1 Answers1

0

changing 'Order in Layer' may work fine.

Select your image from Hierarchy and in Inspector->Sprite Renderer->Order in Layer change the order of layer from 0 to 1 or 2 and run your game to check if it works.

Umair Gul
  • 352
  • 1
  • 4
  • 15