Can't Return a Value for Drop Down List

I’ve followed every single drop down tutorial available on youtube and through google searches. I follow them step by step, line by line. And literally nothing works. No matter what I do, I can’t get my code to return a value for my drop down lists. It’s always just zero.

The simplest code I followed is essentially:

public class DropDown : MonoBehaviour
{
public void HandleInputData(int val)
{
Debug.Log("val: " + val);
}
}

Assigning HandleInputData to the drop down list’s “On Value Change” simply results a post of “val: 0” to the debug log every time I select a new option in the list when test running the scene. I’ve tried this with a completely fresh scene that had no other elements. Since it is posting to the debug, I know that the drop down list is at least accessing the code there, but no matter what variation of tutorial I’ve followed (some with very different code than the above), they all result in the same return of “0” in all instances.

I’m clearly doing something wrong, but like I said, I’ve literally followed multiple different tutorials multiple different times from scratch, and every time it’s the same result.

Edit: Followed a different tutorial and found the missing element: I needed to assign the drop downs onto the canvas. Apparently the first three tutorials I followed all skipped this step.

There are two ways you can attach this service routine to your DropDown (or Slider): one is very useful, the other is not useful and will return a static number (like 0) and force you to go get the value yourself.

This is how to properly choose the “useful” way. Let’s say you are servicing this Dropdown with this script:

using UnityEngine;

public class DropDownService : MonoBehaviour
{
    public void HandleInput(int val)
    {
        Debug.Log( "Dropdown:" + val);
    }
}

When you drag that script onto a GameObject, then drag the GameObject (NOT THE SCRIPT) onto the Dropdown’s event box, make sure you connect it to the Dynamic callbacks, NOT to the Static callbacks, like so:

ALSO: Beware of this gotcha:

3 Likes