Is there a way I can make one script for all my buttons in my game?
So I am working on a simple game right now just to get my foot in the door, and I was wondering if there was a way to make one script that is able to determine what button is being clicked and then take you to that menu. I have 3 scenes in my game right now with 3 buttons. Two on my startScene and one on my instructionsScene (images below)
I currently have a 2 scripts: One for the play button to take you to the gameScene and the second one is for the back button on the instructions scene to take you back to the startScene.
startGame.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class startGame : MonoBehaviour {
public Button playButton;
// Use this for initialization
void Start ()
{
Button playBtn = playButton.GetComponent<Button>();
playBtn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick ()
{
SceneManager.LoadScene("gameScene");
Debug.Log("It works!");
}
// Update is called once per frame
void Update () {
}
}
backButtonInstructions.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class backButtonInstructions : MonoBehaviour {
public Button backButton;
// Use this for initialization
void Start ()
{
Button backBtn = backButton.GetComponent<Button>();
backBtn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
SceneManager.LoadScene("startScene");
Debug.Log("It works!");
}
// Update is called once per frame
void Update () {
}
}
So I am wondering if there is a way to have just one script called button.cs or something and have it determine what button is clicked and then go to the correct scene based on that. I'm new to C# and semi-new to Unity so any help would be greatly appreciated.
Answer by MacDx · Sep 21, 2018 at 08:59 PM
Yes you can if the only thing you want to vary between them is functionality (If you want them for instance to have different behaviours regarding style, animation and such then you'll probably need different scripts for those).
You could have one script, a manager script, that contains all the methods that your buttons will need. Something like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonManager : MonoBehaviour {
public void OnBackButton()
{
SceneManager.LoadScene("startScene");
Debug.Log("It works!");
}
public void OnStartButton()
{
SceneManager.LoadScene("gameScene");
Debug.Log("It works!");
}
}
Then all you need to do is assign this methods in the inspector of each button (I'm thinking that doing it through code like you're doing now is overkill since their functionality seems static, start button will always start the game, when you want a button to change the functionality during run time then you use code to add and remove listeners, IMO).
Although if you are going to have too many buttons, 100 buttons for example, then having a single script with every button method is not good, you would have to break it into menus or screens maybe, so a script for a menu contains all the methods for the buttons in that menu.
The point is that you do not need to create a script for every button.
Hope this helps!
This definitely helps. So I would still need to make a public Button for each button, and also define it in the start method like I previously did?
Nope, once you add the script I wrote to an object, click your button object in the hierarchy so the inspector pops up. Then on the Button component look for the section that says OnClick() click the +(plus sign) button so a field appears. Drag the object with the Button$$anonymous$$anager script to that field and then click on the drop down menu. Look for Button$$anonymous$$anager, then that will open another dropdown with all the methods on that script. Select the appropriate method and done.
Ok. So when I click on the area for the methods what do I choose? This is the list I get:
bool enabled
string name
bool runInEdit$$anonymous$$ode
string tag
bool useGUILayout
Broadcast$$anonymous$$essage (string)
CancelInvoke (string)
CancelInvoke()
Send$$anonymous$$essage (string)
Send$$anonymous$$essageUpwards (string)
StopAllCoroutines ()
StopCoroutine (string)
All of this is under the button$$anonymous$$anager script. What am I suppose to choose?
Looks like you didn't add the methods or you didn't select the correct component one of those 2
After work I will walk you through exactly what I did. Really want to get this working today.
Your answer
Follow this Question
Related Questions
Certain scripts that are attached does not work after build, scene change and closing unity 0 Answers
Performance Improvement For Time Controlled Slider? 2 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer
How to render text on top of the objects that are generated on the map using a script? 1 Answer
Issues using two scripts. 1 Answer