Unity, In Android onClick.AddListener is not working, but in editor its working - c#

Yesterday its working well but today when i open the unity and taken build, onClick.AddListener is not working in Android, but it working in Unity Editor.
void Start(){
instance = this;
guestBtn.onClick.AddListener (GuestBtnFun);
}
public void GuestBtnFun()
{
guestBackBtn.SetActive (true);
pic.gameObject.SetActive (true);
nameText.gameObject.SetActive (true);
shareBtn.gameObject.SetActive (true);
if (!GameSaver.instance.isTutorialShow)
tutorialPanel.SetActive (true);
}

Related

The game is stopped at 0th scene

I made a gamemanager script for my game and add it at 0th scene.
When the game is start and 0th scene is open, the gamemanager script is activated and have to load 1th scene.
It worked well at first. But when I added the code about google play login, it didn't work and don't load 1th scene. Seems like it just stopped.
public class GameMgr : MonoBehaviour {
public static GameMgr instance;
// Use this for initialization
void Start () {
DontDestroyOnLoad(this);
instance = this;
SceneManager.LoadScene("Menu");
PlayGamesPlatform.Activate();
Initialize();
Social.localUser.Authenticate((bool success) =>
{
if (success)
{
Debug.Log("Sign in successful!");
}
else
{
Debug.LogWarning("Failed to sign in with Google Play");
}
});
}
// Update is called once per frame
void Update () {
}
public void Initialize()
{
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
}
I deleted the code about game data that store at this script

Unity - Background Music through scenes with UI slider

I created a game in Unity with 4 different scenes (start, login, options, game itself).
With an empty game object (in the start scene) and the DontDestroyOnLoad function I have managed to have the music played throughout all scenes without stopping or loading new in every scene.
In the options scene there is a slider hooked to an master audio mixer, which works so far.
The only problem for me is that the slider can "interfere" with the gameobject in the start scene (background music, which should be triggered through the slider).
It would be awesome if someone could help me! :)
Here some extracts:
ChangeVolume class:
public AudioMixer audioMixer;
public void setVolume(float volume){
audioMixer.SetFloat ("volume", volume);
}
and
MusicBehaviour class:
//Play global
private static MusicBehaviour instance = null;
public static MusicBehaviour Instance {
get {
return instance;
}
}
void Awake()
{
if (instance != null && instance != this) {
Destroy (this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad (this.gameObject);
}
//Play Global End
//Update is called once per frame
void Update () {
}
I'm excited for your help/solutions, maybe there is a simpler one! :-)
The easiest approach is to use PlayerPrefs and save the sound value there. Every time you start the game in Awake() you will setup the value and when slider is triggered you change the value in PlayerPrefs and set it to the MusicBehaviour instance.
I fixed my problem through editing my scripts in the following way:
Music Behaviour class:
void Update () {
float vol = ChangeVolume.vol;
this.gameObject.GetComponent<AudioSource> ().volume = vol;
}
Change Volume class:
public void setVolume(float volume){
vol = volume;
}
public static float vol = 1.0f;
I also deleted my audiomixer due to not needing it anymore. And it works just fine! :-)

Unity 3D 5.4 2D Collisions Not Working?

I am working on a very small Unity 5.4 project, I have several "pellets" which in the game world have a Rigid Body 2D and 2D polygon collider attached. I then also have a ball which has a Rigid Body 2D, Polygon Colider and also a Physics Material which allows the ball to bounce into other objects.
The following code is attached to a script on the ball would do something... anything when it hits the green pellets but nothing happens. I don't actually want it to quit the application this was just for example purposes.
The following are the properties of both objects:
public class BallBounce : MonoBehaviour {
// Use this for initialization
void Start () {
//activate camera because I am lazy
var cam = GameObject.Find ("camera");
var ball = GameObject.Find ("ball");
Rigidbody2D rb = GetComponent<Rigidbody2D>();
cam.SetActive (true);
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bricks") {
Application.Quit ();
}
}
}
Application.Quit(); does not work in the Editor. It is used for Standalone Build. Replace that with UnityEditor.EditorApplication.isPlaying = false;
Now, if you want it to work in both Editor and Standalone Build:
void Start ()
{
//activate camera because I am lazy
var cam = GameObject.Find ("camera");
var ball = GameObject.Find ("ball");
Rigidbody2D rb = GetComponent<Rigidbody2D>();
cam.SetActive (true);
Debug.Log("Start Called!");
}
void Update ()
{
}
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Collision detected: "+col.gameObject.name);
if (col.gameObject.tag == "Bricks") {
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
Next time when testing if something is true or false, simply use Debug.Log("It works");

How do I add an animated video as a splash screen in Unity3D android game?

I'm using Unity3D for my android game. I want to add a video as an animated splash screen. I've tried different methods to do so. My code in C# looks like this:
public class Splash : MonoBehaviour
{
void Start ()
{
StartCoroutine ("PlayMovie");
}
public IEnumerator PlayMovie ()
{
Handheld.PlayFullScreenMovie ("splash.mp4", Color.black, FullScreenMovieControlMode.Hidden, FullScreenMovieScalingMode.AspectFit);
yield return new WaitForSeconds (1);
Application.LoadLevel ("home");
}
}
You should use it like this
public bool started = false;
void Update ()
{
if (started == false) {
started = true;
StartCoroutine (playMovie ());
}
}
public IEnumerator PlayMovie ()
{
yield Handheld.PlayFullScreenMovie ("splash.mp4",
Color.black,FullScreenMovieControlMode.Hidden,
FullScreenMovieScalingMode.AspectFit);
Application.LoadLevel ("home");
}
you should also check for the documentation
https://docs.unity3d.com/ScriptReference/Handheld.PlayFullScreenMovie.html

statement after waitforseconds() not working in unity

I m new to unity and c# as well. I want to delay a function hence using WaitForSeconds() but my problem is that the statement after WaitForSeconds() is not executed and hence there is no delay shown.
Below is the code:
public void GameOver(){
StartCoroutine (Load ());
Debug.Log("loadDelay");
}
IEnumerator Load(){
Debug.Log ("enum");
yield return new WaitForSeconds(3);
Debug.Log("waited");
}
the output on console shows:
enum
loadDelay
At the same time without any delay when the GameOver() is called and Debug.Log("waited");
is not executed at all. I really don't understand the problem. Please explain if I'm doing something wrong.
Thanks
Code where GameOver() is called:
public class Collision : MonoBehaviour {
public GameObject explosion;
BgScroll bg;
void Start () {
bg = GetComponent<BgScroll> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Enemy") {
//Debug.Log("destroyed");
explosion.renderer.sortingLayerName = "foreground";
Instantiate (explosion, transform.position, Quaternion.identity);
Destroy (gameObject);
Debug.Log("destroyed");
bg.GameOver();
}
}
}
According to Unity's Documentation for WaitForSeconds you are doing it right. and your code works good when i tried to run it. There must be some other problem, or you might have other logs before "waited". I got output in console window as
enum
loadDelay
waited
and obviously "waited" will be printed after 3 seconds if your player is running at that time.

Resources