Porblem with game once builded - c#

in my game to pass from one level to another you must answer some questions. Each question has an associated image. For each level of play there is a different set of questions. During the Unity game simulation there is no problem, but when I play the build of the game, the set of questions is not updated between one level and another, in fact the last question made at the first level is re-proposed.
public class MiniGameManager : MonoBehaviour
{
public Question[] questions;
private static List<Question> unanswered;
private Question currentQuestion;
[SerializeField]
private Image sprite;
[SerializeField]
private float timeQuestion = 1f;
public static int contCorrect;
public Button firstSelected;
void Start()
{
if(unanswered == null || unanswered.Count == 0)
{
unanswered = questions.ToList<Question>();
}
contCorrect = 0;
SetCurrentQuestion();
firstSelected.Select();
}
void SetCurrentQuestion()
{
int index = UnityEngine.Random.Range(0, unanswered.Count);
currentQuestion = unanswered[index];
sprite.sprite = currentQuestion.questionSprite;
}
IEnumerator ToQuestion()
{
unanswered.Remove(currentQuestion);
yield return new WaitForSeconds(timeQuestion);
SetCurrentQuestion();
}
public void UserSelectFirst()
{
if (currentQuestion.answer == 0)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectSecond()
{
if (currentQuestion.answer == 1)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectThird()
{
if (currentQuestion.answer == 2)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectFourth()
{
if (currentQuestion.answer == 3)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
}

I solved the problem. I just had to clear the list of questions before moving to the next level

Related

Prefab instantiating outside of viewport, and giving null reference exception in Unity C#

So, I'm aware that null reference exceptions are a pretty commonly brought up issue on here, but I'm genuinely stuck on this, as this code went from working fine to really not working, and I can't figure out why. I used this tutorial as a starter to get to where I am, https://www.youtube.com/watch?v=2TYLBusJKjc, and then added/adapted for my purposes. I then copied the buttonlistcontrol and buttonlistbutton script files, and renamed and edited them to use as a separate settings menu, which is when it all went downhill.
[SerializeField]
public GameObject upButtonTemplate, buttonTemplate, headingTemplate;
public void GenButtons()
{
ClearAll();
bankIms = Resources.LoadAll("buttons/bank", typeof(Sprite));
coinIms = Resources.LoadAll("buttons/coin", typeof(Sprite));
bgIms = Resources.LoadAll("BgPrevs", typeof(Sprite));
upgrades = new string[]{"text,some more text, even more text none of which matters here"};
upPrices[0] = gameManager.bank;
GenHeading("UPGRADES");
for (int i = 0; i < upgrades.Length; i++)
{
GameObject button = Instantiate(upButtonTemplate) as GameObject;
button.SetActive(true);
Debug.Log("button: " + button);
button.GetComponent<ButtonListButton>().ButtonSetup("upgrade", i); // Null Reference occuring on this line
button.GetComponent<ButtonListButton>().SetUpText(upgrades[i]);
button.GetComponent<ButtonListButton>().SetPrice(upPrices[i]);
button.transform.SetParent(upButtonTemplate.transform.parent,false);
upButts.Add(button);
}
void GenHeading(string head)
{
GameObject heading = Instantiate(headingTemplate) as GameObject;
heading.SetActive(true);
heading.GetComponent<TextMeshProUGUI>().text = head;
heading.transform.SetParent(headingTemplate.transform.parent,false);
headings.Add(heading);
}
When GenButtons is run now, the heading is still being created as expected, but I get an error Object reference not set to an instance of an object, and a clone of the button prefab gets added outside the canvas.
Heading added as expected, but StoreListUpgrade is wildly and annoyingly off
Any help that could sort it would be thoroughly appreciated.
Below is the ButtonListButton script, if it helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ButtonListButton : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI upButtText, buttText;
[SerializeField]
private ButtonListControl buttonControl;
[SerializeField]
private Image buttonImage;
[SerializeField]
private SoundManager soundManager;
public GameManager gameManager;
public double buttonPrice;
public int buttonID;
public bool owned;
public bool selected;
public string buttonType;
public void ButtonSetup(string type, int id)
{
if (type == "bank") {
owned = buttonControl.bankOwn[id];
selected = buttonControl.bankSel[id];
} else if (type == "coin") {
owned = buttonControl.coinOwn[id];
selected = buttonControl.coinSel[id];
} else if (type == "bg") {
owned = buttonControl.bgOwn[id];
selected = buttonControl.bgSel[id];
}
buttonType = type;
buttonID = id;
}
public void SetPrice(double price)
{
if (owned) {
if (selected) {
buttText.text = "✔";
} else {
buttText.text = "Owned";
}
} else {
buttonPrice = price;
buttText.text = "$" + price.ToString("N0");
}
TextColour();
}
public void PriceUpdate(bool onPurchase, bool onSetup)
{
if (buttonType == "upgrade"){
if (buttonID == 0) {
buttonPrice = 0;
SetPrice(0);
} else if (buttonID == 1) {
buttonPrice = (double)(buttonPrice * 1.2f);
buttonControl.upPrices[buttonID] = buttonPrice;
SetPrice(buttonPrice);
} else if (buttonID == 2) {
buttonPrice = (double)(buttonPrice * 1.5f);
buttonControl.upPrices[buttonID] = buttonPrice;
SetPrice(buttonPrice);
} else if (buttonID == 3) {
buttonPrice = (double)(buttonPrice * 1.5f);
buttonControl.upPrices[buttonID] = buttonPrice;
SetPrice(buttonPrice);
}
} else {
if (onPurchase) {
buttText.text = "✔";
selected = true;
owned = true;
buttonControl.OwnSel(buttonType, buttonID, "sel", true);
buttonControl.OwnSel(buttonType, buttonID, "own", true);
buttonPrice = 0;
} else {
if (onSetup == false && selected) {
selected = false;
buttonControl.OwnSel(buttonType, buttonID, "sel", false);
buttText.text = "Owned";
}
}
}
TextColour();
}
public void SetImage(Object ob)
{
Debug.Log("Set Image: " + ob);
buttonImage.sprite = ob as Sprite;
}
public void SetUpText(string upgrade)
{
upButtText.text = upgrade;
}
public void TextColour()
{
if (gameManager.bank >= buttonPrice && buttonPrice > 0 || owned) {
buttText.color = Color.black;
} else {
buttText.color = new Color(180, 0, 0, 1);
}
}
public void OnClick()
{
Debug.Log("bank: " + gameManager.bank + ", buttprice: " + buttonPrice + ", compareto: " + gameManager.bank.CompareTo(buttonPrice));
Debug.Log("adjust compare: " + gameManager.bank.CompareTo(buttonPrice - 0.001f) + "bank: " + gameManager.bank + ", buttprice-: " + (buttonPrice - 0.001f));
if (gameManager.bank.CompareTo(buttonPrice - 0.001f) >= 0) {
if (buttonID == 0 && gameManager.bank == 0) {
soundManager.playnobuy(1);
} else {
soundManager.playmenuopen(0.8f);
buttonControl.ButtonClicked(buttonPrice, buttonType, buttonID);
PriceUpdate(true,false);
}
} else {
soundManager.playnobuy(1);
}
}
}
I think the formatting went a bit off there, apologies for that.

how to make a Delaunay triangulation in unity

So I'm trying to make my own Delaunay triangulation maker fist I need to make triangular in my code. I do this by finding 3 obj and adding them to a list. Know there are two things I do not know how to do first I need to see if that obj is on the list next. I need to do that for every obj.
[System.Serializable]
public class tringle
{
public GameObject GameOBJ_0, GameOBJ_1, GameOBJ_2;
private Vector3 pos_0;
private Vector3 pos_1;
private Vector3 pos_2;
public Vector3 midpoint;
public void SetGameOBJ_0(GameObject setOBj_0)
{
GameOBJ_0 = setOBj_0;
}
public void SetGameOBJ_1(GameObject setOBj_1)
{
GameOBJ_1 = setOBj_1;
}
public void SetGameOBJ_2(GameObject setOBj_2)
{
GameOBJ_2 = setOBj_2;
}
public void SetPos()
{
pos_0= GameOBJ_0.transform.localPosition;
pos_1= GameOBJ_1.transform.localPosition;
pos_2= GameOBJ_2.transform.localPosition;
FindMidPoint();
}
public void FindMidPoint()
{
midpoint = new Vector3((pos_0.x + pos_1.x + pos_2.x)/3,0, (pos_0.z + pos_1.z + pos_2.z) / 3);
}
}
and
foreach (GameObject room in Rooms)
{
for (int i = 0; i < Rooms.Length / 3; i++)
{
if (TRINGLE[i].GameOBJ_0 != room && TRINGLE[i].GameOBJ_1 != room && TRINGLE[i].GameOBJ_2 != room)
{
foreach (tringle item in TRINGLE)
{
if (!TRINGLE.Contains(item)) TRINGLE.Add(item);
}
}
}
}`
public GameObject GameOBJ_0, GameOBJ_1, GameOBJ_2;
private Vector3 pos_0;
private Vector3 pos_1;
private Vector3 pos_2;
public Vector3 midpoint;
public void SetGameOBJ_0(GameObject setOBj_0)
{
GameOBJ_0 = setOBj_0;
}
public void SetGameOBJ_1(GameObject setOBj_1)
{
GameOBJ_1 = setOBj_1;
}
public void SetGameOBJ_2(GameObject setOBj_2)
{
GameOBJ_2 = setOBj_2;
SetPos();
}
public void SetPos()
{
pos_0= GameOBJ_0.transform.localPosition;
pos_1= GameOBJ_1.transform.localPosition;
pos_2= GameOBJ_2.transform.localPosition;
FindMidPoint();
}
public void FindMidPoint()
{
midpoint = new Vector3((pos_0.x + pos_1.x + pos_2.x)/3,0, (pos_0.z + pos_1.z + pos_2.z) / 3);
}
void Update()
{
foreach (GameObject room in Rooms)
{
if(i < triglenuber)
{
foreach (tringle item in TRINGLE)
{
if (!OBJPos.Contains(room))
{
if (TRINGLE[i].GameOBJ_0 == null)
{
OBJPos.Add(room);
TRINGLE[i].SetGameOBJ_0(room);
}
else if (TRINGLE[i].GameOBJ_1 == null)
{
OBJPos.Add(room);
TRINGLE[i].SetGameOBJ_1(room);
}
else if (TRINGLE[i].GameOBJ_2 == null)
{
OBJPos.Add(room);
TRINGLE[i].SetGameOBJ_2(room);
}
else
{
i++;
}
}
}
}
}

Scene is Loading in Unity but unable to Load Scene in Android

My Game is working perfectly in Unity but when I export apk tp Android Mobile 2nd Scene doesn't load properly and does not work.
Please look at the image. It is the second Scene and not loaded properly and the Run button is not working which is working perfectly in unity play mode.
enter image description here
CODES ARE BELOW
SCENE 1 CODE#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_ANALYTICS
using UnityEngine.Analytics;
#endif
#if UNITY_PURCHASING
using UnityEngine.Purchasing;
#endif
public class StartButton : MonoBehaviour
{
public void StartGame()
{
if (PlayerData.instance.ftueLevel == 0)
{
PlayerData.instance.ftueLevel = 1;
PlayerData.instance.Save();
#if UNITY_ANALYTICS
AnalyticsEvent.FirstInteraction("start_button_pressed");
#endif
}
#if UNITY_PURCHASING
var module = StandardPurchasingModule.Instance();
#endif
SceneManager.LoadScene("main");
}
}
SCENE 2 CODE#
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
#if UNITY_ANALYTICS
using UnityEngine.Analytics;
#endif
/// <summary>
/// State pushed on the GameManager during the Loadout, when player select player, theme and accessories
/// Take care of init the UI, load all the data used for it etc.
/// </summary>
public class LoadoutState : AState
{
public Canvas inventoryCanvas;
[Header("Char UI")]
public Text charNameDisplay;
public RectTransform charSelect;
public Transform charPosition;
[Header("Theme UI")]
public Text themeNameDisplay;
public RectTransform themeSelect;
public Image themeIcon;
[Header("PowerUp UI")]
public RectTransform powerupSelect;
public Image powerupIcon;
public Text powerupCount;
public Sprite noItemIcon;
[Header("Accessory UI")]
public RectTransform accessoriesSelector;
public Text accesoryNameDisplay;
public Image accessoryIconDisplay;
[Header("Other Data")]
public Leaderboard leaderboard;
public MissionUI missionPopup;
public Button runButton;
public GameObject tutorialBlocker;
public GameObject tutorialPrompt;
public MeshFilter skyMeshFilter;
public MeshFilter UIGroundFilter;
public AudioClip menuTheme;
[Header("Prefabs")]
public ConsumableIcon consumableIcon;
Consumable.ConsumableType m_PowerupToUse = Consumable.ConsumableType.NONE;
protected GameObject m_Character;
protected List<int> m_OwnedAccesories = new List<int>();
protected int m_UsedAccessory = -1;
protected int m_UsedPowerupIndex;
protected bool m_IsLoadingCharacter;
protected Modifier m_CurrentModifier = new Modifier();
protected const float k_CharacterRotationSpeed = 45f;
protected const string k_ShopSceneName = "shop";
protected const float k_OwnedAccessoriesCharacterOffset = -0.1f;
protected int k_UILayer;
protected readonly Quaternion k_FlippedYAxisRotation = Quaternion.Euler (0f, 180f, 0f);
public override void Enter(AState from)
{
tutorialBlocker.SetActive(!PlayerData.instance.tutorialDone);
tutorialPrompt.SetActive(false);
inventoryCanvas.gameObject.SetActive(true);
missionPopup.gameObject.SetActive(false);
charNameDisplay.text = "";
themeNameDisplay.text = "";
k_UILayer = LayerMask.NameToLayer("UI");
skyMeshFilter.gameObject.SetActive(true);
UIGroundFilter.gameObject.SetActive(true);
// Reseting the global blinking value. Can happen if the game unexpectedly exited while still blinking
Shader.SetGlobalFloat("_BlinkingValue", 0.0f);
if (MusicPlayer.instance.GetStem(0) != menuTheme)
{
MusicPlayer.instance.SetStem(0, menuTheme);
StartCoroutine(MusicPlayer.instance.RestartAllStems());
}
runButton.interactable = false;
runButton.GetComponentInChildren<Text>().text = "Loading...";
if(m_PowerupToUse != Consumable.ConsumableType.NONE)
{
//if we come back from a run and we don't have any more of the powerup we wanted to use, we reset the powerup to use to NONE
if (!PlayerData.instance.consumables.ContainsKey(m_PowerupToUse) || PlayerData.instance.consumables[m_PowerupToUse] == 0)
m_PowerupToUse = Consumable.ConsumableType.NONE;
}
Refresh();
}
public override void Exit(AState to)
{
missionPopup.gameObject.SetActive(false);
inventoryCanvas.gameObject.SetActive(false);
if (m_Character != null) Addressables.ReleaseInstance(m_Character);
GameState gs = to as GameState;
skyMeshFilter.gameObject.SetActive(false);
UIGroundFilter.gameObject.SetActive(false);
if (gs != null)
{
gs.currentModifier = m_CurrentModifier;
// We reset the modifier to a default one, for next run (if a new modifier is applied, it will replace this default one before the run starts)
m_CurrentModifier = new Modifier();
if (m_PowerupToUse != Consumable.ConsumableType.NONE)
{
PlayerData.instance.Consume(m_PowerupToUse);
Consumable inv = Instantiate(ConsumableDatabase.GetConsumbale(m_PowerupToUse));
inv.gameObject.SetActive(false);
gs.trackManager.characterController.inventory = inv;
}
}
}
public void Refresh()
{
PopulatePowerup();
StartCoroutine(PopulateCharacters());
StartCoroutine(PopulateTheme());
}
public override string GetName()
{
return "Loadout";
}
public override void Tick()
{
if (!runButton.interactable)
{
bool interactable = ThemeDatabase.loaded && CharacterDatabase.loaded;
if(interactable)
{
runButton.interactable = true;
runButton.GetComponentInChildren<Text>().text = "Run!";
//we can always enabled, as the parent will be disabled if tutorial is already done
tutorialPrompt.SetActive(true);
}
}
if(m_Character != null)
{
m_Character.transform.Rotate(0, k_CharacterRotationSpeed * Time.deltaTime, 0, Space.Self);
}
charSelect.gameObject.SetActive(PlayerData.instance.characters.Count > 1);
themeSelect.gameObject.SetActive(PlayerData.instance.themes.Count > 1);
}
public void GoToStore()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(k_ShopSceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive);
}
public void ChangeCharacter(int dir)
{
PlayerData.instance.usedCharacter += dir;
if (PlayerData.instance.usedCharacter >= PlayerData.instance.characters.Count)
PlayerData.instance.usedCharacter = 0;
else if(PlayerData.instance.usedCharacter < 0)
PlayerData.instance.usedCharacter = PlayerData.instance.characters.Count-1;
StartCoroutine(PopulateCharacters());
}
public void ChangeAccessory(int dir)
{
m_UsedAccessory += dir;
if (m_UsedAccessory >= m_OwnedAccesories.Count)
m_UsedAccessory = -1;
else if (m_UsedAccessory < -1)
m_UsedAccessory = m_OwnedAccesories.Count-1;
if (m_UsedAccessory != -1)
PlayerData.instance.usedAccessory = m_OwnedAccesories[m_UsedAccessory];
else
PlayerData.instance.usedAccessory = -1;
SetupAccessory();
}
public void ChangeTheme(int dir)
{
PlayerData.instance.usedTheme += dir;
if (PlayerData.instance.usedTheme >= PlayerData.instance.themes.Count)
PlayerData.instance.usedTheme = 0;
else if (PlayerData.instance.usedTheme < 0)
PlayerData.instance.usedTheme = PlayerData.instance.themes.Count - 1;
StartCoroutine(PopulateTheme());
}
public IEnumerator PopulateTheme()
{
ThemeData t = null;
while (t == null)
{
t = ThemeDatabase.GetThemeData(PlayerData.instance.themes[PlayerData.instance.usedTheme]);
yield return null;
}
themeNameDisplay.text = t.themeName;
themeIcon.sprite = t.themeIcon;
skyMeshFilter.sharedMesh = t.skyMesh;
UIGroundFilter.sharedMesh = t.UIGroundMesh;
}
public IEnumerator PopulateCharacters()
{
accessoriesSelector.gameObject.SetActive(false);
PlayerData.instance.usedAccessory = -1;
m_UsedAccessory = -1;
if (!m_IsLoadingCharacter)
{
m_IsLoadingCharacter = true;
GameObject newChar = null;
while (newChar == null)
{
Character c = CharacterDatabase.GetCharacter(PlayerData.instance.characters[PlayerData.instance.usedCharacter]);
if (c != null)
{
m_OwnedAccesories.Clear();
for (int i = 0; i < c.accessories.Length; ++i)
{
// Check which accessories we own.
string compoundName = c.characterName + ":" + c.accessories[i].accessoryName;
if (PlayerData.instance.characterAccessories.Contains(compoundName))
{
m_OwnedAccesories.Add(i);
}
}
Vector3 pos = charPosition.transform.position;
if (m_OwnedAccesories.Count > 0)
{
pos.x = k_OwnedAccessoriesCharacterOffset;
}
else
{
pos.x = 0.0f;
}
charPosition.transform.position = pos;
accessoriesSelector.gameObject.SetActive(m_OwnedAccesories.Count > 0);
AsyncOperationHandle op = Addressables.InstantiateAsync(c.characterName);
yield return op;
if (op.Result == null || !(op.Result is GameObject))
{
Debug.LogWarning(string.Format("Unable to load character {0}.", c.characterName));
yield break;
}
newChar = op.Result as GameObject;
Helpers.SetRendererLayerRecursive(newChar, k_UILayer);
newChar.transform.SetParent(charPosition, false);
newChar.transform.rotation = k_FlippedYAxisRotation;
if (m_Character != null)
Addressables.ReleaseInstance(m_Character);
m_Character = newChar;
charNameDisplay.text = c.characterName;
m_Character.transform.localPosition = Vector3.right * 1000;
//animator will take a frame to initialize, during which the character will be in a T-pose.
//So we move the character off screen, wait that initialised frame, then move the character back in place.
//That avoid an ugly "T-pose" flash time
yield return new WaitForEndOfFrame();
m_Character.transform.localPosition = Vector3.zero;
SetupAccessory();
}
else
yield return new WaitForSeconds(1.0f);
}
m_IsLoadingCharacter = false;
}
}
void SetupAccessory()
{
Character c = m_Character.GetComponent<Character>();
c.SetupAccesory(PlayerData.instance.usedAccessory);
if (PlayerData.instance.usedAccessory == -1)
{
accesoryNameDisplay.text = "None";
accessoryIconDisplay.enabled = false;
}
else
{
accessoryIconDisplay.enabled = true;
accesoryNameDisplay.text = c.accessories[PlayerData.instance.usedAccessory].accessoryName;
accessoryIconDisplay.sprite = c.accessories[PlayerData.instance.usedAccessory].accessoryIcon;
}
}
void PopulatePowerup()
{
powerupIcon.gameObject.SetActive(true);
if (PlayerData.instance.consumables.Count > 0)
{
Consumable c = ConsumableDatabase.GetConsumbale(m_PowerupToUse);
powerupSelect.gameObject.SetActive(true);
if (c != null)
{
powerupIcon.sprite = c.icon;
powerupCount.text = PlayerData.instance.consumables[m_PowerupToUse].ToString();
}
else
{
powerupIcon.sprite = noItemIcon;
powerupCount.text = "";
}
}
else
{
powerupSelect.gameObject.SetActive(false);
}
}
public void ChangeConsumable(int dir)
{
bool found = false;
do
{
m_UsedPowerupIndex += dir;
if(m_UsedPowerupIndex >= (int)Consumable.ConsumableType.MAX_COUNT)
{
m_UsedPowerupIndex = 0;
}
else if(m_UsedPowerupIndex < 0)
{
m_UsedPowerupIndex = (int)Consumable.ConsumableType.MAX_COUNT - 1;
}
int count = 0;
if(PlayerData.instance.consumables.TryGetValue((Consumable.ConsumableType)m_UsedPowerupIndex, out count) && count > 0)
{
found = true;
}
} while (m_UsedPowerupIndex != 0 && !found);
m_PowerupToUse = (Consumable.ConsumableType)m_UsedPowerupIndex;
PopulatePowerup();
}
public void UnequipPowerup()
{
m_PowerupToUse = Consumable.ConsumableType.NONE;
}
public void SetModifier(Modifier modifier)
{
m_CurrentModifier = modifier;
}
public void StartGame()
{
if (PlayerData.instance.tutorialDone)
{
if (PlayerData.instance.ftueLevel == 1)
{
PlayerData.instance.ftueLevel = 2;
PlayerData.instance.Save();
}
}
manager.SwitchState("Game");
}
public void Openleaderboard()
{
leaderboard.displayPlayer = false;
leaderboard.forcePlayerDisplay = false;
leaderboard.Open();
}
}
When you are building an APK file it can be splited to APK + OBB.
So, it is possible that you are trying to load a scene that is absent in your APK. You need to force the creation of the APK without the OBB. To do it you need to check Player Settings in Android, go to Publishing Settings and uncheck Split Application Binary.

trying to save and load a value in c# unity but having trouble

I am trying to get part of my game working but having trouble solving this problem!
I have boosters that remove game pieces if the user has enough in game coins to use. It is working to an extent and once a booster has been used the coins will decrement but upon exiting and reloading the game it does not update the correct value!
For example, the boosters can be used at a cost of 50 coins, if I have 200 coins and use 4 boosters the value updates to 0 and this works. However if I restart then the value of 200 is still there!
I have a coin manager script set up with playerprefs that gives the user 50 coins after each level, this is linked to the booster script.
Booster class:
public class Booster : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {
Image m_image;
RectTransform m_rectXform;
Vector3 m_startPosition;
Board m_board;
Tile m_tileTarget;
public static GameObject ActiveBooster;
public Text instructionsText;
public string instructions = "drag over game piece to remove";
public bool isEnabled = false;
public bool isDraggable = false;
public bool isLocked = false;
public List<CanvasGroup> canvasGroups;
public UnityEvent boostEvent;
public int boostTime = 15;
//coin test
CoinManager coinManager;
private void Awake()
{
m_image = GetComponent<Image> ();
m_rectXform = GetComponent<RectTransform> ();
m_board = Object.FindObjectOfType<Board> ().GetComponent<Board> ();
coinManager = GameObject.Find ("CoinManager").GetComponent<CoinManager> ();
}
void Start ()
{
EnableBooster (false);
}
public void EnableBooster(bool state)
{
isEnabled = state;
if (state)
{
DisableOtherBoosters ();
Booster.ActiveBooster = gameObject;
}
else if(gameObject == Booster.ActiveBooster)
{
Booster.ActiveBooster = null;
}
m_image.color = (state) ? Color.white : Color.gray;
if (instructionsText != null)
{
instructionsText.gameObject.SetActive (Booster.ActiveBooster != null);
if (gameObject == Booster.ActiveBooster)
{
instructionsText.text = instructions;
}
}
}
void DisableOtherBoosters()
{
Booster[] allBoosters = Object.FindObjectsOfType<Booster> ();
foreach (Booster b in allBoosters)
{
if (b != this)
{
b.EnableBooster (false);
}
}
}
public void ToggleBooster()
{
EnableBooster (!isEnabled);
}
public void OnBeginDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked)
{
m_startPosition = gameObject.transform.position;
EnableCanvasGroups (false);
}
}
public void OnDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked && Camera.main != null)
{
Vector3 onscreenPosition;
RectTransformUtility.ScreenPointToWorldPointInRectangle (m_rectXform, eventData.position,
Camera.main, out onscreenPosition);
gameObject.transform.position = onscreenPosition;
RaycastHit2D hit2D = Physics2D.Raycast (onscreenPosition, Vector3.forward, Mathf.Infinity);
if (hit2D.collider != null)
{
m_tileTarget = hit2D.collider.GetComponent<Tile> ();
}
else
{
m_tileTarget = null;
}
}
}
public void OnEndDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked)
{
gameObject.transform.position = m_startPosition;
EnableCanvasGroups (true);
if (m_board != null && m_board.isRefilling)
{
return;
}
if (m_tileTarget != null)
{
if (boostEvent != null)
{
boostEvent.Invoke (); //can do things here like play a sound effect
}
EnableBooster (false);
m_tileTarget = null;
Booster.ActiveBooster = null;
}
}
}
void EnableCanvasGroups(bool state)
{
if (canvasGroups != null && canvasGroups.Count > 0)
{
foreach (CanvasGroup cGroup in canvasGroups)
{
if (cGroup != null)
{
cGroup.blocksRaycasts = state;
}
}
}
}
public void RemoveOneGamePiece()
{
if (m_board != null && m_tileTarget != null)
{
if (coinManager.currentCoinCount >= 50)
{
m_board.ClearAndRefillBoard (m_tileTarget.xIndex, m_tileTarget.yIndex);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
public void AddTime()
{
if (GameManager.Instance != null)
{
if (coinManager.currentCoinCount >= 50){
GameManager.Instance.AddTime (boostTime);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
public void DropColorBomb() //big bomb
{
if (m_board != null && m_tileTarget != null)
{
if (coinManager.currentCoinCount >= 50)
{
m_board.MakeColorBombBooster (m_tileTarget.xIndex, m_tileTarget.yIndex);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
}
CoinManager class:
public class CoinManager : Singleton<CoinManager>{
// reference to the UI Text element
public Text coinText;
// current number of coins earned during gameplay
public int currentCoinCount = 0;
// because we don't have a key set, we should create it and initialize its value to 0
public void InitCoinCount()
{
if (!PlayerPrefs.HasKey("TotalCoins"))
{
PlayerPrefs.SetInt("TotalCoins",0);
}
}
// returns the number of coins stored in PlayerPrefs
public int GetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
return PlayerPrefs.GetInt("TotalCoins");
}
// if we don't have the key set, so return 0
return 0;
}
// sets a number of coins into PlayerPrefs if the current coin count is greater
public void SetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
int oldCoins = PlayerPrefs.GetInt("TotalCoins");
if (currentCoinCount > oldCoins)
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}
}
}
// display the coin count as a TextUI
public void ShowCoinCount()
{
if (coinText != null)
{
coinText.text = currentCoinCount.ToString();
}
}
//new method for oldcoin count
public void AddCoins(int coins)
{
currentCoinCount = currentCoinCount + coins;
}
}
// sets a number of coins into PlayerPrefs if the current coin count is greater
Well, this comment basically tells you the problem. If the coin count isn't greater than what was saved previously, it doesn't save!
Change this:
// sets a number of coins into PlayerPrefs if the current coin count is greater
public void SetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
int oldCoins = PlayerPrefs.GetInt("TotalCoins");
if (currentCoinCount > oldCoins)
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}
}
}
To this:
// sets a number of coins into PlayerPrefs
public void SetCoinCount()
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}

How to Detect single and Double click in unity?

I've written the following code:
void OnMouseUpAsButton()
{
if (Type == 0) {
if (click && Time.time <= (clickTime + clickDelta))
{
Debug.Log("Double");
Destroy(sp);
click = false;
}
else
{
Debug.Log("one");
click = true;
clickTime = Time.time;
Destroy(sp);
}
}else if (Type == -1)
{
Destroy(sp);
}
}
It detects double-click But has a problem!
in Both, objects will be removed
When you first click happens, Object is deleted And the second click is not recognized!
I want for Android
please help me. thanks
The following class can be used in Editor or device.
public class InputController : MonoBehaviour
{
public event Action OnSingleTap;
public event Action OnDoubleTap;
[Tooltip("Defines the maximum time between two taps to make it double tap")]
[SerializeField]private float tapThreshold = 0.25f;
private Action updateDelegate;
private float tapTimer = 0.0f;
private bool tap = false;
private void Awake()
{
#if UNITY_EDITOR || UNITY_STANDALONE
updateDelegate = UpdateEditor;
#elif UNITY_IOS || UNITY_ANDROID
updateDelegate = UpdateMobile;
#endif
}
private void Update()
{
if(updateDelegate != null){ updateDelegate();}
}
private void OnDestroy()
{
OnSingleTap = null;
OnDoubleTap = null;
}
#if UNITY_EDITOR || UNITY_STANDALONE
private void UpdateEditor()
{
if (Input.GetMouseButtonDown(0))
{
if (Time.time < this.tapTimer + this.tapThreshold)
{
if(OnDoubleTap != null){ OnDoubleTap(); }
this.tap = false;
return;
}
this.tap = true;
this.tapTimer = Time.time;
}
if (this.tap == true && Time.time>this.tapTimer + this.tapThreshold)
{
this.tap = false;
if(OnSingleTap != null){ OnSingleTap();}
}
}
#elif UNITY_IOS || UNITY_ANDROID
private void UpdateMobile ()
{
for(int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 2)
{
if(OnDoubleTap != null){ OnDoubleTap();}
}
if(Input.GetTouch(i).tapCount == 1)
{
if(OnSingleTap != null) { OnSingleTap(); }
}
}
}
}
#endif
}
You're telling it to destroy the object in both halves of your if-statement (re-read your inner else block). You would need to set up your update method or coroutine to process a single click after the Double Click timer has been reached.
Simple sample below:
void OnMouseUpAsButton()
{
if(!clicked)
{
clicked = true;
return;
}
if(Time.time <= (clickTime + clickDelta))
{
//Double Click occured
clicked = false;
}
}
void Update()
{
if(clicked)
{
if(Time.time >= (clickTime + clickDelta))
{
//Handle single click
clicked = false;
}
}
}
Note this is just to demonstrate a simple way to handle this using most of what you provided.
You can also find additional information at this question:
http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html

Categories