Unity script work on Editor but not working in Android - c#

Hello I'm game developer one day when I want to test my game on android I join to my game but when I press on buttons public void function doesn't work I fixed it by add to scripts what using buttons onClick.AddListener() but one problem IAP don't work with onClick.AddListener() you can help me fix problem with public void function what works on Editor but don't work in Android when I press on buttons. Thanks.
Script with AddListener [This script where I add onClick.AddListener() to fix this problem but it's be uncomfortable thats why I want to fix this]:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameButtonManager : MonoBehaviour
{
public GameObject DonationShop;
public GameObject ShopBtn;
public GameObject QuitShopBtn;
public GameObject QuitBtn;
public bool CanPress = true;
void Update()
{
ShopBtn.GetComponent<Button>().onClick.AddListener(EnableShop);
QuitShopBtn.GetComponent<Button>().onClick.AddListener(DisableShop);
QuitBtn.GetComponent<Button>().onClick.AddListener(BackToMenu);
}
public void EnableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(true);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
public void BackToMenu()
{
SceneManager.LoadScene(1);
}
public IEnumerator CanPressEnable()
{
yield return new WaitForSeconds(5.5f);
CanPress = true;
}
public void DisableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(false);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
}

Related

How to load death scene in C#/unitygames?

haven't posted on here before but I have been trying for a while to create a game and would like a death/game over sort of scene to appear when the player loses all 3 of their lives. I have a functioning game manager and my player can lose lives (they have 3). This is all being done in unity games and is 2d (idk if that helps). I currently have other stuff in my scene loader script that works fine so I will post the whole thing but I am having issues with the bottom most code!
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public string scenename;
public GameManager GM;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
SceneManager.LoadScene(scenename);
}
}
private void Deathscene()
{
if(GM.LifeTotal == 0)
{
SceneManager.LoadScene(Bob);
}
}
}
Gamemanager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int PotionsCollected = 0;
public int LifeTotal = 3;
public Text PotionsOutput;
public Text LifeOutput;
void Update()
{
PotionsOutput.text = "Potions: " + PotionsCollected;
LifeOutput.text = "Life: " + LifeTotal;
}
public void CollectPotion()
{
PotionsCollected++;
}
public void UsePotion()
{
PotionsCollected--;
}
public void LoseLife()
{
LifeTotal--;
}
}
What you can do is from your Unity Editor go to File->Build Settings and then drag and drop inside the active scenes window your death scene.
Then an index will be generated on the right side of the window and you can use that index to load the scene. like this:
SceneManager.LoadScene("Use your generated index");
NOTE: Use your index as a number and not as a string.
UPDATED Solution:
public void LoseLife()
{
LifeTotal--;
if(LifeTotal <= 0)
{
SceneManager.LoadScene("Use your generated index");
}
}
I supposse LoseLife() it's called when the enemy attacks your player.

OnTriggerEnter2D doesn't work after switching scenes

I have a game that is similar to "Flappy Bird" and I have main menu where I can start game and change skin of a pigeon. My skin collection is implemented with scroll rect and in the center there is a trigger which starts an animation of scaling a pigeon, it works fine until I click "start" and the scene changes to game and when I return to my main menu and click "skins" this trigger doesn't work anymore.
Script what is attached to all scroll rect elements to detect collisions with trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResizeFieldScript : MonoBehaviour
{
private Animator _anim;
private void Start()
{
_anim = GetComponent<Animator>();
}
public void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Trigger is working");
if(collider.tag == "ResizeField")
{
Debug.Log("Condition is working");
_anim.SetBool("isInTrigger", true);
}
}
public void OnTriggerExit2D(Collider2D collider)
{
_anim.SetBool("isInTrigger", false);
}
}
Script what is attached to an empty object to change scenes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class UIController : MonoBehaviour
{
[SerializeField] private List<string> sceneNameList;
private string sceneToFind;
private int index = 0;
public void SceneChanger()
{
sceneToFind = EventSystem.current.currentSelectedGameObject.name;
foreach(string str in sceneNameList)
{
if(str == sceneToFind)
{
SceneManager.LoadScene(index);
index = 0;
break;
}
index++;
}
}
public void Exit()
{
Application.Quit();
}
public void BackMenu()
{
SceneManager.LoadScene(4);
}
}
OnTriggerEnter2D doesn't work after switching scenes. We could use OnTriggerEnter2D to jump scenes.
code show as below:
private void Update() {
// If E is pressed
if (Input. GetKeyDown(KeyCode. E)) {
// scene switching
SceneManager.LoadScene(4);
}
}
private void OnTriggerEnter2D(Collider collision) {
if (collision. tag == "ResizeField") {
// The UI prompts the user to press E to jump
EnterDialog.SetActive(true);
Debug.Log("Condition is working");
// _anim.SetBool("isInTrigger", true);
}
}
Hope it helps you.

coroutine animation not playing as expected

so everything seems to be running as far as the AI switching different States, however when it gets in range with the player the State freezes in attack and does not play any Attack Animation, I have created the AI with Bone Rigging, Im wondering if this may be affecting it, here is my script for calling the Animation and the script for one the animation has finished to go back to idleFollow state.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeleeWeapon : Weapon
{
[SerializeField] private float attackDelay = 1f;
private Collider2D damageAreaCollider2D;
private Animator animatorAttack;
private bool attacking;
private readonly int useMeleeWeapon = Animator.StringToHash(name:"UseMeleeWeapon");
private void Start()
{
damageAreaCollider2D = GetComponent<BoxCollider2D>();
animatorAttack = GetComponent<Animator>();
//animatorAttack.Play(useMeleeWeapon);
}
public override void UseWeapon()
{
StartCoroutine(routine: Attack());
}
/*protected override void Update()
{
base.Update();
// FlipMeleeWeapon();
}*/
private IEnumerator Attack()
{
if (attacking)
{
yield break;
}
// Attack
attacking = true;
damageAreaCollider2D.enabled = true;
animatorAttack.Play(useMeleeWeapon);
// Stop Attack
yield return new WaitForSeconds(attackDelay);
damageAreaCollider2D.enabled = false;
attacking = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "AI/Decisions/Attack Completed", fileName = "AttackCompleted")]
public class DecisionAttackCompleted : AIDecision
{
public override bool Decide(StateController controller)
{
return AttackCompleted(controller);
}
private bool AttackCompleted(StateController controller)
{
if (controller.CharacterWeapon.CurrentWeapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length
> controller.CharacterWeapon.CurrentWeapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).normalizedTime)
{
return true;
}
return false;
}
}
Ok I fixed the issue, I ended up placing this code into the WeaponMelee script, but now im dealing with a weird glitch where the AI keeps tring to face the left instead of the player when they are close to eachother. this has to do with the boxcollider2D so ive placed a capsulecollider2d inside of him and changed the script to capsulecollider2d as well. but the issue is still happening
private void OnTriggerEnter2D(Collider2D collision)
{
StartCoroutine(routine: Attack());
}

How do i use PlayOneShot() on Unity?

I've been trying to use this function to trigger an sound when entering a trigger but i can't make it work. I've read and read my code and nothing seems off, looks exactly like the documentation says so i just can't figure out my error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
private AudioClip clip;
public bool nextTrig;
void Start()
{
nextTrig = false;
clip = soundObject.GetComponent<AudioClip>();
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
nextTrig = true;
trigger.SetActive(false);
}
}
}
The AudioSource is atached to another object. The trigger is supposed to play the sound after another event happens. The trigger part works fine because nextTrig is set to true like intended, but the sound doesn't play. Also, the sound itself works fine too and with a nice volume.
it does not work because, what is this ?
clip = soundObject.GetComponent<AudioClip>();
there is no component called AudioClip.
for audio clip get it from prefabs directly to the script or if you want to get it from the audio source that you have in the soundObject, then it will be like :
clip = soundObject.GetComponent<AudioSource>().clip;
and later you will play it with PlayOneShot which will be waste of time, because you are playing the same clip which is originally taken from this audio source. which actually need just this line to play it
soundObject.GetComponent<AudioSource>().Play();
finally if you are trying to get the audio clip in the script from the prefabs your code will be like :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
public AudioClip clip;
public bool nextTrig;
void Start()
{
nextTrig = false;
//don't forget to assign the your audio clip to the script from the prefabs
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
if (clip =! null)
soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
nextTrig = true;
trigger.SetActive(false);
}
}
}
and if you already have the audio clip in audio source which is attached to the sound object then your code will be :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
public bool nextTrig;
void Start()
{
nextTrig = false;
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
soundObject.GetComponent<AudioSource>().Play();
nextTrig = true;
trigger.SetActive(false);
}
}
}

How to enable/disable scripts in 1 gameObject?

I have gameObject MainCamera and there is script LookAtCamera as my main view option and I have script MouseLook that I wanna make secondary with right-click.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
LookAtCamera.enabled = false;
MouseLook.enabled = true;
}
}
}
How to declare script as a public component of MainCamera? I just have one of them enabled and switch between with mouse right click.
You've got most of it done. So, declare the scripts as a public variable like below, and then assign them on the inspector:
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
}
}
}
Thx :) I didn't know how to declare script. I also added else to go back to default camera setup.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
} else {
mouseLookScript.enabled = false;
lookAtScript.enabled = true;
}
}
}

Categories