0
\$\begingroup\$

For each of my scenes, I have an empty GameObject _Manager that stores my scripts for setting up the level, keeping track of variables, etc.

On my main screen, I have a play button that isn't instantiated; I simply dragged it into the editor. In my script Main.cs I have a function Play() which calls LoadLevel("Game"). To get my play button to call the Play function, I dragged _Manager into the onClick slot and selected the function. This worked fine and I have a functional play button.

Unfortunately, this is not working for the prefabbed buttons I need to instantaite in my Game scene. In Game.cs I have a function LayoutButtons() which is both public and returns void, yet I am unable to drag _Manager into the slot on my prefabbed button. Even if I do it before converting the button to a prefab, it simply disappears once it becomes a prefab.

I've managed to work around this by putting my LayoutButtons() function in a new script which I attached directly to the prefab, but I'd like to keep the function within Game.cs and attached to my manager object if that's possible.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You want to Setup play as a public void Play() so when you drag the manager to the click event you can see Play() on the list.

Edit: If you are using a prefabbed button you want to also use a prefabbed manager and drag that into the button click event to work properly.

public void Play()
{
     LoadLevel("Game");
}
\$\endgroup\$
2
  • \$\begingroup\$ Perhaps my wall of text was confusing. Your code is exactly what I have working. The problem I'm having is that I have other buttons (prefabbed) that don't allow me to do this same thing. \$\endgroup\$
    – Matt
    Commented Jul 12, 2015 at 19:52
  • \$\begingroup\$ you would need to have the manager prefabbed and drag the prefabbed version into the click event to call. \$\endgroup\$ Commented Jul 12, 2015 at 20:36
1
\$\begingroup\$

If you instantiate the button you can get a reference to it, like GameObject _Button = Instantiate(_ButtonPrefab) and then you can call _Button.AddListener(//A Method on this script) to add a new method to the button, which just does the same as clicking on the plus, dragging in a game object and choosing a method.

I don't know if this is still helpful for you or not, but it's better to use SceneManager to load scenes, instead of LoadLevel, because LoadLevel is old.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .