how to make a Delaunay triangulation in unity - c#

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++;
}
}
}
}
}

Related

How do I fix music in my Unity project which randomly bugs when I run the Gameplay scene?

So I recently finished Sebastian Lague's Create a Game series and everything works fine except for the music. I don't know the cause of issue but while in gameplay scene another theme just starts playing, same thing happens when I restart the game and this also happens in Sebastian's finale version of the game so I can't find the answer there. I didn't really try anything since I don't have an idea what cause of the issue may be, so I decided to write here and maybe someone will know the answer. Thanks in advance!
Audio Manager:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class AudioManager : MonoBehaviour
{
public enum AudioChannel { Master, Sfx, Music };
public float masterVolumePercent { get; private set; }
public float sfxVolumePercent { get; private set; }
public float musicVolumePercent { get; private set; }
private AudioSource sfx2DSource;
private AudioSource[] musicSources;
private int activeMusicSourceIndex;
public static AudioManager instance;
private Transform audioListener;
private Transform playerT;
private SoundLibrary library;
private void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
library = GetComponent<SoundLibrary>();
musicSources = new AudioSource[2];
for (int i = 0; i < 2; i++)
{
GameObject newMusicSource = new GameObject("Music source " + (i + 1));
musicSources[i] = newMusicSource.AddComponent<AudioSource>();
newMusicSource.transform.parent = transform;
}
GameObject newSfx2Dsource = new GameObject("2D sfx source");
sfx2DSource = newSfx2Dsource.AddComponent<AudioSource>();
newSfx2Dsource.transform.parent = transform;
audioListener = FindObjectOfType<AudioListener>().transform;
if (FindObjectOfType<PlayerInput>() != null)
{
playerT = FindObjectOfType<PlayerInput>().transform;
}
masterVolumePercent = PlayerPrefs.GetFloat("master vol", 1);
sfxVolumePercent = PlayerPrefs.GetFloat("sfx vol", 1);
musicVolumePercent = PlayerPrefs.GetFloat("music vol", 1);
}
}
void Update()
{
if (playerT != null)
{
audioListener.position = playerT.position;
}
}
public void SetVolume(float volumePercent, AudioChannel channel)
{
switch (channel)
{
case AudioChannel.Master:
masterVolumePercent = volumePercent;
break;
case AudioChannel.Sfx:
sfxVolumePercent = volumePercent;
break;
case AudioChannel.Music:
musicVolumePercent = volumePercent;
break;
}
musicSources[0].volume = musicVolumePercent * masterVolumePercent;
musicSources[1].volume = musicVolumePercent * masterVolumePercent;
PlayerPrefs.SetFloat("master vol", masterVolumePercent);
PlayerPrefs.SetFloat("sfx vol", sfxVolumePercent);
PlayerPrefs.SetFloat("music vol", musicVolumePercent);
PlayerPrefs.Save();
}
public void PlayMusic(AudioClip clip, float fadeDuration = 1)
{
activeMusicSourceIndex = 1 - activeMusicSourceIndex;
musicSources[activeMusicSourceIndex].clip = clip;
musicSources[activeMusicSourceIndex].Play();
StartCoroutine(AnimateMusicCrossfade(fadeDuration));
}
public void PlaySound(AudioClip clip, Vector3 pos)
{
if (clip != null)
{
AudioSource.PlayClipAtPoint(clip, pos, sfxVolumePercent * masterVolumePercent);
}
}
public void PlaySound(string soundName, Vector3 pos)
{
PlaySound(library.GetClipFromName(soundName), pos);
}
public void PlaySound2D(string soundName)
{
sfx2DSource.PlayOneShot(library.GetClipFromName(soundName), sfxVolumePercent * masterVolumePercent);
}
IEnumerator AnimateMusicCrossfade(float duration)
{
float percent = 0;
while (percent < 1)
{
percent += Time.deltaTime * 1 / duration;
musicSources[activeMusicSourceIndex].volume = Mathf.Lerp(0, musicVolumePercent * masterVolumePercent, percent);
musicSources[1 - activeMusicSourceIndex].volume = Mathf.Lerp(musicVolumePercent * masterVolumePercent, 0, percent);
yield return null;
}
}
private void OnLevelFinishedLoading(Scene scene, LoadSceneMode sceneMode)
{
if (playerT == null)
{
if (FindObjectOfType<PlayerInput>() != null)
playerT = FindObjectOfType<PlayerInput>().transform;
}
}
}
Music Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MusicManager : MonoBehaviour
{
[SerializeField] private AudioClip mainTheme;
[SerializeField] private AudioClip menuTheme;
private string sceneName;
private void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
void PlayMusic()
{
AudioClip clipToPlay = null;
if(sceneName == "Main Menu")
{
if(clipToPlay == null)
clipToPlay = menuTheme;
}
else if(sceneName == "Gameplay")
{
if(clipToPlay == null)
clipToPlay = mainTheme;
}
if(clipToPlay != null)
{
AudioManager.instance.PlayMusic(clipToPlay, 2);
Invoke("PlayMusic", clipToPlay.length);
Debug.Log(clipToPlay.length);
}
}
private void OnLevelFinishedLoading(Scene scene, LoadSceneMode sceneMode)
{
if(sceneName != scene.name)
{
sceneName = scene.name;
Invoke("PlayMusic", .2f);
}
}
}

Unity - Weird StackOverflow Exception thrown in output_log.txt after build

Today I'm coming to you because I have a weird StackOverflow Exception and do not know how to fix it at all...
First off, this seems to only happen on windows after I build the game.
This is what I see in the output_log.txt :
onMoneyChanged is being called! (4145)
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
Player:set_Money(Int32) (at /Users/Name/Desktop/My Game/Assets/Scripts/Mobs/Player.cs:89)
Coin:OnPickup(ItemCollector) (at /Users/Name/Desktop/My Game/Assets/Scripts/Items/Coin.cs:12)
ItemCollector:Update() (at /Users/Name/Desktop/My Game/Assets/Scripts/Items/ItemCollector.cs:35)
(Filename: /Users/Name/Desktop/My Game/Assets/Scripts/Mobs/Player.cs Line: 89)
onMoneyChanged is being called! (4150)
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
Player:set_Money(Int32) (at /Users/Name/Desktop/My Game/Assets/Scripts/Mobs/Player.cs:89)
Coin:OnPickup(ItemCollector) (at /Users/Name/Desktop/My Game/Assets/Scripts/Items/Coin.cs:12)
ItemCollector:Update() (at /Users/Name/Desktop/My Game/Assets/Scripts/Items/ItemCollector.cs:35)
(Filename: /Users/Name/Desktop/My Game/Assets/Scripts/Mobs/Player.cs Line: 89)
Uploading Crash Report
StackOverflowException: The requested operation caused a stack overflow.
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
at (wrapper delegate-invoke) System.Action:invoke_void__this__ ()
I have looked everywhere and can't seem to understand where it comes from. I might not be seeing something very simple...
Here is the player script:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Events;
using PixelUtilities;
public class Player : Mob, IUpgradable, IXmlSerializable {
#region Static Section
private static readonly AnimationParameter Skill2SpeedId = "Skill 2 Speed";
private static readonly string[] AttackAxisNames = new string[] {
null,
"Attack1",
"Attack2",
"Attack3"
};
//It seems this must be longer in duration than the transition into the attack state, due to the potential transition interruption sources!
private const float AttackUnjoggableTime = 0.65f;
#endregion
[Header("Player")]
[SerializeField] private float skill2Speed = 1;
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundCheckRadius = 0.25f;
[SerializeField] private LayerMask groundLayers;
[SerializeField] private bool isGrounded;
[SerializeField] GameObject weaponSpecial;
private new Rigidbody2D rigidbody;
private new BoxCollider2D collider;
private float horizontal;
private float smoothedHorizontal;
private Vector3 velocity;
private int superCharge; //Represents the number of enemies defeated that counts towards allowing the player to use their super attack.
private bool canIncreaseSuperCharge = true;
private bool canSuperSmash;
private int money = 0;
//private int roundMoney = 0;
private int[] levels = new int[2];
//DO NOT show in the inspector. That will make it changeable without actually updating the data
//that needs to change based based on this upgradeLevel.
private int baseDamage;
private float timeLastAttacked;
public event Action onMoneyChanged;
public bool CanSuperSmash {
get { return canSuperSmash; }
set {
//Prevent from redundant setting, because we'll need to know
//just when the value changed from false to true
if (canSuperSmash == value)
return;
canSuperSmash = value;
weaponSpecial.SetActive(value);
if (value)
superCharge = GameManager.Mage.SpecialEnemyCount;
}
}
public BoxCollider2D Collider {
get { return collider; }
}
public int SuperCharge {
get { return superCharge; }
}
public int LevelCount {
get { return levels.Length; }
}
public int Money {
get { return money; }
set {
if (GameManager.IsDebugMode)
Debug.Log("Setting the player's money from " + money + " to " + value + ".");
money = value;
Debug.Log("onMoneyChanged is being called! (" + money + ")");
if (onMoneyChanged != null)
onMoneyChanged();
}
}
//public int RoundMoney {
// get { return roundMoney; }
// set {
// roundMoney = value;
// Debug.Log("roundMoney has been set to " + roundMoney + ".");
// }
//}
public override void Reset() {
base.Reset();
groundLayers = LayerMask.GetMask("Ground");
}
public override void Awake() {
base.Awake();
collider = GetComponentInChildren<BoxCollider2D>();
HPStatus.onDeath += OnDeath;
baseDamage = StandTallCurves.GetNthStepInEnemyHealthCurve(0) / 2;
}
public override void Start() {
rigidbody = GetComponent<Rigidbody2D>();
}
public int GetLevel(int levelIndex) {
return levels[levelIndex];
}
public void SetLevel(int levelIndex, int value) {
value = Mathf.Max(0, value);
levels[levelIndex] = value;
switch (levelIndex) {
case 0:
baseDamage = StandTallCurves.GetNthStepInEnemyHealthCurve(value) / 2;
break;
case 1:
HPStatus.HP = HPStatus.MaxHP = StandTallCurves.GetNthStepInEnemyHealthCurve(value);
break;
}
}
public override void StartAttack(int attackNumber) {
base.StartAttack(attackNumber);
timeLastAttacked = Time.time;
}
protected override void OnUpdate() {
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayers);
if (CanPerformActions)
AcceptAttackInput();
AcceptMovementInput();
//AcceptJumpInput();
//AcceptRotationalInput();
if (GameManager.IsDeveloperMode) {
if (Input.GetKeyDown(KeyCode.C)) {
Money += 1000;
}
if (Input.GetKeyDown(KeyCode.Y)) {
CanSuperSmash = true;
}
if (Input.GetKeyDown(KeyCode.T) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand))) {
Time.timeScale = (Time.timeScale == 1) ? 5 : 1;
}
}
}
private void AcceptMovementInput() {
horizontal = hInput.GetAxis("Horizontal");
//smoothedHorizontal = Input.GetAxis("Horizontal");
velocity = rigidbody.velocity;
velocity.x = Mathf.Sign(horizontal) * FinalMovementSpeed;
rigidbody.velocity = velocity;
//Section for updating their rotation
if (horizontal < 0) {
Quaternion newRotation = transform.rotation;
newRotation.y = 180;
transform.rotation = newRotation;
}
if (horizontal > 0) {
Quaternion newRotation = transform.rotation;
newRotation.y = 0;
transform.rotation = newRotation;
}
}
private void AcceptAttackInput() {
for (int i = 1; i <= 2; i++) {
if (hInput.GetButtonDown(AttackAxisNames[i])) {
StartAttack(i);
return;
}
}
//Attack 3 is special for the player -- their super smash -- and is handled differently.
if (hInput.GetButtonDown(AttackAxisNames[3])) {
if (canSuperSmash) {
StartSuperSmash();
} else {
if (GameManager.IsDebugMode) {
Debug.Log("Attempted to super smash, but " + name + " was unable to super smash!");
}
}
}
}
protected override float CalculateFinalMovementSpeed() {
float finalMovementSpeed = Mathf.Abs(horizontal) * MoveSettings.MoveSpeed;
finalMovementSpeed *= MovementFactor;
return finalMovementSpeed;
}
protected override void UpdateContinuousAnimatorParameters() {
animator.SetBool(Animations.IsMovingId, Time.time - timeLastAttacked > AttackUnjoggableTime && Mathf.Abs(horizontal) > 0.08f && Mathf.Abs(velocity.x) > 0.08f);
//animator.SetFloat(Skill2SpeedId, skill2Speed);
}
/* Uncomment to enable jumping
private void AcceptJumpInput() {
if (Input.GetButtonDown("Jump") && isGrounded) {
rigidbody.velocity = new Vector3(rigidbody.velocity.x, JumpFactor * MoveSettings.JumpSpeed, 0);
if (JumpFactor > 0)
animator.SetTrigger(JumpId);
}
}*/
/// <summary>
/// This tells the whether or not the super charge count can be increased.
/// </summary>
public void SetSuperChargeActive(bool value) {
canIncreaseSuperCharge = value;
}
public bool GainSuperCharge() {
return GainSuperCharge(1);
}
public bool GainSuperCharge(int amount) {
//If they're already charged up, then don't allow superCharge to increment
if (!GameManager.Mage.gameObject.activeSelf || !canIncreaseSuperCharge || canSuperSmash)
return false;
superCharge = Mathf.Clamp(superCharge + amount, 0, GameManager.Mage.SpecialEnemyCount);
if (superCharge == GameManager.Mage.SpecialEnemyCount) {
CanSuperSmash = true; //Important to call the C# property here, NOT directly access the field, "canSuperSmash".
}
return true;
}
private void StartSuperSmash() {
SetSuperChargeActive(false);
CanSuperSmash = false;
StartCoroutine(AttackAfterDelay(3, 0)); //0 was initially 0.8f;
}
public void ClearSuperCharge() {
if (GameManager.IsDebugMode)
Debug.Log("superCharge set to 0!");
superCharge = 0;
}
private IEnumerator AttackAfterDelay(int attackNumber, float initialDelay) {
if (initialDelay > 0)
yield return new WaitForSeconds(initialDelay);
StartAttack(attackNumber);
}
public override void OnDrawGizmosSelected() {
base.OnDrawGizmosSelected();
if (groundCheck != null) {
Gizmos.color = new Color(0.6f, 1, 0, 1);
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
attackChecker.DrawGizmos(Color.red);
}
private void InstantKillAllInView() {
Camera camera = Camera.main;
Vector3 cameraPos = camera.transform.position;
float height = 2 * camera.orthographicSize;
float width = height * camera.aspect; //h * (w/h) = w
RaycastHit2D[] hits = Physics2D.BoxCastAll((Vector2) cameraPos, new Vector2(width, height), 0, Vector2.right, 0.01f, enemyLayers, -0.01f, 0.01f);
for (int i = 0; i < hits.Length; i++) {
Mob target = hits[i].transform.GetComponent<Mob>();
if (target == null)
continue;
target.InstantSuperKill(this);
}
}
public void DamageOthersInRangeFromPlayer() {
DamageOthersInRange(baseDamage);
}
private void OnDeath(DamageInfo finalDamage) {
GameManager.StartGameOverScreen();
}
public void Load(XElement element) {
if (element == null)
return;
int intValue;
XElement child = element.Element("Money");
if (child != null && int.TryParse(child.Value, out intValue))
Money = intValue;
child = element.Element("HealthUpgrade");
if (child != null) {
//Old serialized layout
//if (int.TryParse(child.Value, out intValue))
// HealthUpgradeLevel = intValue;
//child = element.Element("DamageUpgrade");
//if (child != null && int.TryParse(child.Value, out intValue))
// DamageUpgradeLevel = intValue;
} else {
//New serialized layout
child = element.Element("Levels");
if (child != null)
LoadLevels(child, ref levels);
}
}
public XElement Save() {
XElement element = new XElement(GetType().Name);
element.Add(new XElement("Money", money));
//e.Add(new XElement("HealthUpgrade", healthUpgradeLevel));
//e.Add(new XElement("DamageUpgrade", damageUpgradeLevel));
element.Add(SaveLevels(ref levels));
return element;
}
#region Public Animator Methods
public void AnimatorAllowSuperSmashToRestart() {
ClearSuperCharge();
SetSuperChargeActive(true);
}
#endregion
}
Here is the Coin script:
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Coin : MonoBehaviour, IItem {
[SerializeField] private int moneyAmount = 5;
public void OnValidate() {
moneyAmount = Mathf.Max(0, moneyAmount);
}
public void OnPickup(ItemCollector collector) {
collector.Owner.Money += moneyAmount;
//collector.Owner.RoundMoney += moneyAmount;
}
}
And finally, the itemCollector script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemCollector : MonoBehaviour {
[SerializeField] private float radius = 0.5f;
[SerializeField] private LayerMask itemMask;
private Player owner;
private Collider2D[] results = new Collider2D[16];
private int resultsCount;
public Player Owner {
get { return owner; }
}
public void Awake() {
owner = GetComponentInParent<Player>();
if (owner == null) {
if (GameManager.IsDebugMode) {
Debug.LogError(" require a " + typeof(Player).Name + " to be in a parent!" +
"\nThe " + GetType().Name + " will be destroyed, as it would not be able to function.");
}
DestroyImmediate(gameObject);
}
}
public void Update() {
resultsCount = Physics2D.OverlapCircleNonAlloc(transform.position, radius, results, itemMask);
for (int i = 0; i < resultsCount; i++) {
IItem item = results[i].GetComponent<IItem>();
if (item != null) {
MonoBehaviour m = item as MonoBehaviour;
if (m != null && m.enabled) {
item.OnPickup(this);
m.enabled = false;
GameObject.Destroy(m.gameObject);
}
}
}
}
public void OnDrawGizmosSelected() {
Gizmos.DrawWireSphere(transform.position, radius);
}
}
Thank you in advance for you help.
Regards!
For anyone who has a problem like that.
I fixed the problem. I had a .onMoneyChanged in an update function that was making the game crash.
I moved it to the onEnable and onDisable method and now it works just fine.

Prevent Empty Game Room in Unity Network Lobby from Destroying

Software: Unity 2018.1
Language: C# 4
Plugin: Network Lobby
Problem/Question:
I have my Unity Network Lobby setup and working. I'm trying to prevent the Game Room from Destroying when all players leave the room or disconnect so that it can always be returned to at a later time.
Attempts:
I tried changing int i = 0 to into i = -1 (Didn't work, no affect)
I tried changing if (p != null) to if (p == null) (Didn't work, no affect)
I tried silencing the code as well with 'Command, /'. (Didn't work, no affect)
Here's the Code:
public override void OnLobbyServerPlayerRemoved(NetworkConnection conn, short playerControllerId)
{
for (int i = 0; i < lobbySlots.Length; ++i)
{
LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
if (p != null)
{
p.RpcUpdateRemoveButton();
p.ToggleJoinButton(numPlayers + 1 >= minPlayers);
}
}
}
public override void OnLobbyServerDisconnect(NetworkConnection conn)
{
for (int i = 0; i < lobbySlots.Length; ++i)
{
LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
if (p != null)
{
p.RpcUpdateRemoveButton();
p.ToggleJoinButton(numPlayers >= minPlayers);
}
}
}
Complete Code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.Match;
using System.Collections;
namespace Prototype.NetworkLobby
{
public class LobbyManager : NetworkLobbyManager
{
static short MsgKicked = MsgType.Highest + 1;
static public LobbyManager s_Singleton;
[Header("Unity UI Lobby")]
[Tooltip("Time in second between all players ready & match start")]
public float prematchCountdown = 5.0f;
[Space]
[Header("UI Reference")]
public LobbyTopPanel topPanel;
public RectTransform mainMenuPanel;
public RectTransform lobbyPanel;
public LobbyInfoPanel infoPanel;
public LobbyCountdownPanel countdownPanel;
public GameObject addPlayerButton;
protected RectTransform currentPanel;
public Button backButton;
public Text statusInfo;
public Text hostInfo;
//Client numPlayers from NetworkManager is always 0, so we count
(throught connect/destroy in LobbyPlayer) the number
//of players, so that even client know how many player there is.
[HideInInspector]
public int _playerNumber = 0;
//used to disconnect a client properly when exiting the matchmaker
[HideInInspector]
public bool _isMatchmaking = false;
protected bool _disconnectServer = false;
protected ulong _currentMatchID;
protected LobbyHook _lobbyHooks;
void Start()
{
s_Singleton = this;
_lobbyHooks = GetComponent<Prototype.NetworkLobby.LobbyHook>();
currentPanel = mainMenuPanel;
backButton.gameObject.SetActive(false);
GetComponent<Canvas>().enabled = true;
DontDestroyOnLoad(gameObject);
SetServerInfo("Offline", "None");
}
public override void OnLobbyClientSceneChanged(NetworkConnection conn)
{
if (SceneManager.GetSceneAt(0).name == lobbyScene)
{
if (topPanel.isInGame)
{
ChangeTo(lobbyPanel);
if (_isMatchmaking)
{
if (conn.playerControllers[0].unetView.isServer)
{
backDelegate = StopHostClbk;
}
else
{
backDelegate = StopClientClbk;
}
}
else
{
if (conn.playerControllers[0].unetView.isClient)
{
backDelegate = StopHostClbk;
}
else
{
backDelegate = StopClientClbk;
}
}
}
else
{
ChangeTo(mainMenuPanel);
}
topPanel.ToggleVisibility(true);
topPanel.isInGame = false;
}
else
{
ChangeTo(null);
Destroy(GameObject.Find("MainMenuUI(Clone)"));
//backDelegate = StopGameClbk;
topPanel.isInGame = true;
topPanel.ToggleVisibility(false);
}
}
public void ChangeTo(RectTransform newPanel)
{
if (currentPanel != null)
{
currentPanel.gameObject.SetActive(false);
}
if (newPanel != null)
{
newPanel.gameObject.SetActive(true);
}
currentPanel = newPanel;
if (currentPanel != mainMenuPanel)
{
backButton.gameObject.SetActive(true);
}
else
{
backButton.gameObject.SetActive(false);
SetServerInfo("Offline", "None");
_isMatchmaking = false;
}
}
public void DisplayIsConnecting()
{
var _this = this;
infoPanel.Display("Connecting...", "Cancel", () => { _this.backDelegate(); });
}
public void SetServerInfo(string status, string host)
{
statusInfo.text = status;
hostInfo.text = host;
}
public delegate void BackButtonDelegate();
public BackButtonDelegate backDelegate;
public void GoBackButton()
{
backDelegate();
topPanel.isInGame = false;
}
// ----------------- Server management
public void AddLocalPlayer()
{
TryToAddPlayer();
}
public void RemovePlayer(LobbyPlayer player)
{
player.RemovePlayer();
}
public void SimpleBackClbk()
{
ChangeTo(mainMenuPanel);
}
public void StopHostClbk()
{
if (_isMatchmaking)
{
matchMaker.DestroyMatch((NetworkID)_currentMatchID, 0, OnDestroyMatch);
_disconnectServer = true;
}
else
{
StopHost();
}
ChangeTo(mainMenuPanel);
}
public void StopClientClbk()
{
StopClient();
if (_isMatchmaking)
{
StopMatchMaker();
}
ChangeTo(mainMenuPanel);
}
public void StopServerClbk()
{
StopServer();
ChangeTo(mainMenuPanel);
}
class KickMsg : MessageBase { }
public void KickPlayer(NetworkConnection conn)
{
conn.Send(MsgKicked, new KickMsg());
}
public void KickedMessageHandler(NetworkMessage netMsg)
{
infoPanel.Display("Kicked by Server", "Close", null);
netMsg.conn.Disconnect();
}
//===================
public override void OnStartHost()
{
base.OnStartHost();
ChangeTo(lobbyPanel);
backDelegate = StopHostClbk;
SetServerInfo("Hosting", networkAddress);
}
public override void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
base.OnMatchCreate(success, extendedInfo, matchInfo);
_currentMatchID = (System.UInt64)matchInfo.networkId;
}
public override void OnDestroyMatch(bool success, string extendedInfo)
{
base.OnDestroyMatch(success, extendedInfo);
if (_disconnectServer)
{
StopMatchMaker();
StopHost();
}
}
//allow to handle the (+) button to add/remove player
public void OnPlayersNumberModified(int count)
{
_playerNumber += count;
int localPlayerCount = 0;
foreach (PlayerController p in ClientScene.localPlayers)
localPlayerCount += (p == null || p.playerControllerId == -1) ? 0 : 1;
addPlayerButton.SetActive(localPlayerCount < maxPlayersPerConnection && _playerNumber < maxPlayers);
}
// ----------------- Server callbacks ------------------
//we want to disable the button JOIN if we don't have enough player
//But OnLobbyClientConnect isn't called on hosting player. So we override the lobbyPlayer creation
public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject obj = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
LobbyPlayer newPlayer = obj.GetComponent<LobbyPlayer>();
newPlayer.ToggleJoinButton(numPlayers + 1 >= minPlayers);
for (int i = 0; i < lobbySlots.Length; ++i)
{
LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
if (p != null)
{
p.RpcUpdateRemoveButton();
p.ToggleJoinButton(numPlayers + 1 >= minPlayers);
}
}
return obj;
}
public override void OnLobbyServerPlayerRemoved(NetworkConnection conn, short playerControllerId)
{
for (int i = 0; i < lobbySlots.Length; ++i)
{
LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
if (p != null)
{
p.RpcUpdateRemoveButton();
p.ToggleJoinButton(numPlayers + 1 >= minPlayers);
}
}
}
public override void OnLobbyServerDisconnect(NetworkConnection conn)
{
for (int i = 0; i < lobbySlots.Length; ++i)
{
LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
if (p != null)
{
p.RpcUpdateRemoveButton();
p.ToggleJoinButton(numPlayers >= minPlayers);
}
}
}
public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
{
//This hook allows you to apply state data from the lobby-player to the game-player
//just subclass "LobbyHook" and add it to the lobby object.
if (_lobbyHooks)
_lobbyHooks.OnLobbyServerSceneLoadedForPlayer(this, lobbyPlayer, gamePlayer);
return true;
}
// --- Countdown management
public override void OnLobbyServerPlayersReady()
{
bool allready = true;
for(int i = 0; i < lobbySlots.Length; ++i)
{
if(lobbySlots[i] != null)
allready &= lobbySlots[i].readyToBegin;
}
if(allready)
StartCoroutine(ServerCountdownCoroutine());
}
public IEnumerator ServerCountdownCoroutine()
{
float remainingTime = prematchCountdown;
int floorTime = Mathf.FloorToInt(remainingTime);
while (remainingTime > 0)
{
yield return null;
remainingTime -= Time.deltaTime;
int newFloorTime = Mathf.FloorToInt(remainingTime);
if (newFloorTime != floorTime)
{//to avoid flooding the network of message, we only send a notice to client when the number of plain seconds change.
floorTime = newFloorTime;
for (int i = 0; i < lobbySlots.Length; ++i)
{
if (lobbySlots[i] != null)
{//there is maxPlayer slots, so some could be == null, need to test it before accessing!
(lobbySlots[i] as LobbyPlayer).RpcUpdateCountdown(floorTime);
}
}
}
}
for (int i = 0; i < lobbySlots.Length; ++i)
{
if (lobbySlots[i] != null)
{
(lobbySlots[i] as LobbyPlayer).RpcUpdateCountdown(0);
}
}
ServerChangeScene(playScene);
}
// ----------------- Client callbacks ------------------
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
infoPanel.gameObject.SetActive(false);
conn.RegisterHandler(MsgKicked, KickedMessageHandler);
if (!NetworkServer.active)
{//only to do on pure client (not self hosting client)
ChangeTo(lobbyPanel);
//backDelegate = StopClientClbk;
SetServerInfo("Client", networkAddress);
}
}
public override void OnClientDisconnect(NetworkConnection conn)
{
base.OnClientDisconnect(conn);
ChangeTo(mainMenuPanel);
}
public override void OnClientError(NetworkConnection conn, int errorCode)
{
ChangeTo(mainMenuPanel);
infoPanel.Display("Cient error : " + (errorCode == 6 ? "timeout" : errorCode.ToString()), "Close", null);
}
}
}
I think that you have to re-think the concept.
If you want to keep a "reference" to the last scene, you should save the state of your scene in some static manner (json?) or at least in a variable-gameObject.
You won't to keep "OnDestroyOnLoad" your scene, cause it's a waste of resources.
But, if you still want, I'll recommend you to use the additive way to load scenes. So you load the new scene into additive way, and disable the objects of the last scene.
Additive Mode: https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html
Disable GameObjects: https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

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);
}

OnMouseOver In Unity

quick question I got this script here to show the name of an Item you collect. These names are made public so I can change them within the item list. What I want to do Is to walk into an item and hover my mouse over the item so it can display the name of the item in the middle of the screen. I don't know if I should use a trigger, or a GUILayout or however. Please Help and thanks. Here is the Script: UPDATED
public class RayCasting : MonoBehaviour
{
public float pickupDistance;
public List<Item> items;
//public List<Keybutton> buttons;
#region Unity
void Start ()
{
Screen.lockCursor = false;
for (int i = 0; i < items.Count; i++) {
Item temp = items[i];
int randomIndex = Random.Range(i, items.Count);
items[i] = items[randomIndex];
items[randomIndex] = temp;
}
}
void Update ()
{
RaycastHit hit;
Ray ray = new Ray (transform.position, transform.forward);
if (Physics.Raycast (ray, out hit, pickupDistance)) {
foreach (Item item in items) {
if (Input.GetMouseButtonDown (0)) {
if (item.gameObject.Equals (hit.collider.gameObject)) {
numItemsCollected++;
item.Collect ();
break;
}
}
}
}
}
void OnGUI()
{
GUI.backgroundColor = Color.blue;
GUI.Box(new Rect(120,390,170,250),"Text Message");
GUILayout.BeginArea(new Rect(132,432,100,170));
{
GUILayout.BeginVertical();
{
if (numItemsCollected < items.Count)
{
foreach (Item item in items)
GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "" + item.password: " ", item.name ));
}
else
{
foreach (Item item in items)
GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "" + item.password: " ", item.name ));
//GUILayout.Label("Take code to KeyPad");
}
}
GUILayout.EndVertical();
}
GUILayout.EndArea();
//Enter Code to unlock doors.
if (GUI.Button (new Rect (250, 830, 100, 50), "Enter Code"))
if (numItemsCollected > items.Count) {
Debug.Log ("Entering Code");
}
}
#endregion
#region Private
private int numItemsCollected;
#endregion
}
[System.Serializable]
public class Item
{
public string name;
//public GUIText textObject;
public GameObject gameObject;
public float guiDelay = 0.1f;
private float lastHoverTime = -99.0f;
public int password;
public bool Collected { get; private set; }
public void Collect()
{
Collected = true;
//gameObject.SetActive(false);
}
public void passwordNumber()
{
password = 0;
Collected = true;
gameObject.SetActive(false);
}
void OnMouseEnter ()
{
lastHoverTime = Time.timeSinceLevelLoad;
}
void OnGUI(){
if (lastHoverTime + guiDelay > Time.timeSinceLevelLoad) {
GUI.Box (new Rect (300, 300, 170, 250), name);
}
}
}
Attach following script to every item. It will show QUI.Box when mouse hovers the item. Just change the size of the QUI.Box and set the message to suitable values.
using UnityEngine;
using System.Collections;
public class HoverGUI : MonoBehaviour {
public string message = "Foo Bar";
public float guiDelay = 0.1f;
private float lastHoverTime = -99.0f;
void OnMouseOver() {
lastHoverTime = Time.timeSinceLevelLoad;
}
void OnGUI(){
if(lastHoverTime + guiDelay > Time.timeSinceLevelLoad){
GUI.Box(new Rect(0,0,170,250),message);
}
}
}

Categories