Scripts Interfering With Each Other
Hi.
I am just learning Unity so I decided to make a Pong game, I am really really close to finishing it, when I game across something weird
I have 2 scripts, 1 of them Starts the game (I have a Main Menu), the other one Exits. When I assign them to their correct buttons for some reason when I open the .exe file when I click Start it closes the application and when I remove the Exit button altogether the Start button works fine
Yep Here
Start Button: private bool startedLoad;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
if (!startedLoad)
{
Scene$$anonymous$$anager.LoadScene("scene_test_trail1");
startedLoad = true;
}
}
}
Exit Button: private void Update() { if (Input.Get$$anonymous$$ouseButtonDown(0)) { Application.Quit(); } } }
I also found out that if you click in the Canvas (Which for some reason it is the camera view) it does ever just the Start or Exit script. When I look in Canvas and the $$anonymous$$ain Camera none of the scripts are connected
you could do this, use the same script for both as you're doing, but assign the buttons to do what they need to do.
i program in Js : code isn't formatted.
e.g. if(isExitButton == true){ //quit the game. }
if(isPlayButton == true){ // start the game }
Answer by UnityCoach · Jan 15, 2017 at 04:02 PM
I see, you're using UI Buttons within a Canvas, right?
If so, you can put all your UI functions in one script, like UI Manager, then add the component to an empty game object and assign them to the buttons onClick events in the editor.
public class UIManager : MonoBehaviour
{
public bool startedLoad;
public void StartGame ()
{
SceneManager.LoadScene ("scene_test_trail1");
startedLoad = true;
}
public void Quit ()
{
Application.Quit ();
}
}
I am new to Unity and I have like zero to know idea what you mean. I understand to add them both to one script then what do I do?
Ok, I got the GameObject to do On-Click events. I am trying to find out why the Buttons aren't doing anything
There's like 3 layers of UI implementations in Unity now, so it may be a bit confusing if you look at older tutorials. But that is the way to go since Unity 4.6 with the new UI stuff. Glad you got it working.
Your answer
Follow this Question
Related Questions
How to reference script in unity (C#) ? 0 Answers
Image UI not enabling C# SOLVED 1 Answer
How to stop movement script on void start and resume after. 0 Answers
Is there a way I can make one script for all my buttons in my game? 1 Answer
space shooter tutorial issue "Ending the game" unity 2018.2 b 1 Answer