- Home /
The question is answered, right answer was accepted
Button doesn't work when returning to scene (android)
Using Unity 5.6.3p2
Building a game for Android.
I have a scene called "Main" that is active when the app starts. There is a single button called "Create". Button takes you to "Scene2". In Scene2, there is a button called "Save". "Save" does some stuff in a function, and the last line of that function is: SceneManager.LoadScene("Main");
The function works... the things that are supposed to happen happens, and then the user is returned to "Main".
However, Now the "Create" button no longer works. It still highlights when you press it, but it doesn't take you to "Scene2" anymore.
???
My "Main" Scene does have an "EventSystem" and I do have "Force Module Active" in the checked state.
My question is similar to https://answers.unity.com/questions/913093/ui-buttons-stop-working-after-i-load-another-scene.html but I think the answers are not applicable to the current version of Unity.
More details I should probably add:
In my "Main" scene, I have a object I call the "Game Controller". Within this object is a script component and that contains this:
void Awake ()
{
if (Instance == null) {
DontDestroyOnLoad (gameObject);
Instance = this;
} else if (Instance != this) {
Destroy (gameObject);
}
}
(I should also add that I'm fairly new to Unity)
Hi @riotsw I've had a look at the similar question you mentioned. Have you tried the solutions proposed by fregas and Fdudka?
I suspect that this is happening because the "subscription" to the button's onClick
event has been somehow lost. If none of the previous solutions work for you, I could post some code to show you how you can subscribe to the buttons events in code, ins$$anonymous$$d of setting a method to run in the Inspector. Let me know.
Hi @pako There was one comment on that post: "the event system game object was not in a persistent game object in the hiearchy, so it didn't have DontDestroyOnLoad. I just made it part of another GameObject that had DontDestoryOnLoad AND followed Ironcross's advice for "Force $$anonymous$$odule Active" and all was well again"
I think this is probably related to my issue. As mentioned in my OP, I do have a DontDestroyOnLoad thing in a GameController object in my $$anonymous$$ain scene. There is also An EventSystem object in the $$anonymous$$ain scene. But I don't understand how I would attach my EventSystem object to a persistent game object....? That might be what I'm missing (again... fairly new to unity)
Answer by pako · Jan 06, 2018 at 05:19 PM
The DontDestroyOnLoad on your GameController is relevant (i.e. affects) only the GameController. The comment refers to the GameObject of the EventSystem, i.e. the GameObject named EventSystem, with the EventSystem component to it. Although I had second thoughts about it, what the comment meant, was to include another custom script in the EventSystem GameObject, with the DontDestroyOnLoad (gameObject)
statement in Awake()
(as you've done for your GameController).
However, as I've said above, I've had second thoughts about this, since your "Main" Scene already includes an active EventSystem, which responds to the click of your button, and takes you to Scene 2, which also includes an active EventSystem, as you can, again, click on the button and go to back to the "Main" Scene.
Therefore, to start with, the EventSystems in both scenes function properly, so there's no question about using a DontDestroyOnLoad statement to preserve any EventSystem.
Additionally, since the EventSystem in the "Main" Scene starts active, when this scene loads again (after Scene 2) the EventSystem should, be in an active state again.
Just to make sure, please check if the EventSystem is active in the Hierarchy of the "Main" Scene, after returning from Scene 2.
Is there a reason why you are not using one of the latest Unity versions? I am using v.2017.2.1 and I tried to replicate the problem, but couldn't.
I just created a button in an empty scene, and attached the following script to it:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour
{
public int NewScene;
public void ChangeScene()
{
SceneManager.LoadScene(NewScene);
}
}
I configured the button's Inspector to run`ChangeScene()` on each button click, duplicated the scene, and added both scenes to the build settings. I also set the NewScene
variable to '1' in Scene 1, and '0' in Scene 2.
The scenes were changing back and forth just fine, both in Editor Play mode, as well as in the Android build I made.
So, maybe you should try and replicate the above in a new empty project. If you still have a problem, it could be related to the Unity version you are running.
Otherwise, there could be something in your code, which prevents scene 2 to load the second time, in which case, it would be good to post some of your code.
@pako Thanks so much. :) First... regarding the Unity version, when I "check for updates" it returns that I have the latest...?
Then regarding whether or not EventSystem is in an "active" state. How do I know that? When I run it, and go between scenes, nothing seems to change when I look at in the hierarchy (nothing changes with any object in the hierarchy for me).
I will try replicating in an empty project next just the way you described...
oh wait... I see on the website re: 2017. Will try that first. :)
You are running v.5.6.3p2, which is a "patch" version, as signified by the "p2" suffix, and it's not recommended (by Unity) to run patch versions, unless they help solve a particular problem, and even then, only until the next main release comes out. I don't know why "check for updates" reports that you are running the latest version, but "Unity 5" is in version 5.6.5 and "Unity 2017" has been out for quite a while now, the latest version is 2017.3.0: https://unity3d.com/get-unity/download/archive
$$anonymous$$ake a backup copy of your project (just in case) before upgrading, because some of your scripts may be automatically upgraded after you open the new Unity version.
I just mean that the GameObject of the EventSystem will be grayed-out in the Hierarchy, if it is in an "inactive" state. However, if nothing is changing, it seems to be active.
Ok - so I'm on 2017.3.of3 now
$$anonymous$$y original project still has the same problem.
So I made an empty project as you described and made the same SceneChanger script. But I don't see where I put in the NewScene number when I attach the SceneChanger to the OnClick() of my button...?
in my case i still having this issue in android. i tested all the advises you mentioned ...
@khaledr it's better to post a new question, since this one has already been answered. Since you've followed my advise and it's still not working for you, it must be something different, even though it looks the same. Hence the need for a new question.