- Home /
lvl win & lvl lose windows
Hello! Ive stuck with problem to write a code for "LevelWinWindow" & "LevelLoseWindow". Now Im in process about lvllosewindow. And i dont know how create logic that i need. Should appear window with "lose" after char dies, and "win" when he got 100 kills. code for "lose" looks like this: Script HUD.
public class HUD : MonoBehaviour {
[SerializeField]
public GameObject loseWindow;
//private GameObject LevelLoseWindow;
public void ShowWindow(GameObject loseWindow)
{
loseWindow.SetActive(true);
//ControlCH.Instance.State = GameState.Pause;
}
public void ShowLevelLoseWindow()
{
// loseWindow.alpha = 1.0f;
ShowWindow(loseWindow);
}
static private HUD _instance;
public static HUD Instance
{
get
{
return _instance;
}
}
private void Awake()
{
_instance = this;
}
public void ButtonRestart()
{
}
}
And script for Character:
public void GameOver()
{
HUD.Instance.ShowLevelLoseWindow();
}
public void Destroyed()
{
GameOver();
//SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
Any ideas ? plz =(
Answer by Petch12 · Jul 09, 2016 at 08:05 PM
OK, for the char when dead. You want to firstly check the gameObject (char) is not active then if the gameObject (char) is not active show the Lose window (in the char script):
void Lose()
{
if (!gameObject.activeSelf)
{
//show Lose window
}
}
Second, in order to check whether the char has won you are going to need an int which stores the number of kills.:
void Update()
{
if (numOfKills == 100)
{
won();
}
}
void won()
{
//all you need is to show win.
}
Although you will need to increase the number of kills everytime your char kills an enemy.
I hope this is what you meant, if not just say and I'll get back to you.
Thank you, i will try it and then answer you about my result. P.S. my script about score looks like that :
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class ScoreLabel : $$anonymous$$onoBehaviour {
public static int score;
private Text label;
// Use this for initialization
void Start ()
{
label = gameObject.GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
label.text = score.ToString();
}
}
No problem, hope it works for you. (by the way the scripts I attached won't directly copy into your game as they weren't built for it - they are meant only to help you along (and I hope they do!) :). Any other problems just reply
Unfortunately, i don't know what is wrong, logic seems to me normal but it doesn't work ... thats why i think that problem is not in code but maybe in editor... phhhhh i'm losing my motivation =(
Ok sorry my last question about text was bad. (sorry ;) . Is it only 'LoseWindow' that isn't working???
If this is the case I would just create LoseWindow (within canvas - not from prefab) and then just set it active when needed. (I personally don't use SerializeField very often (if at all xD - I probably should though http://forum.unity3d.com/threads/do-you-use-serializefield-a-lot.307079/) so I would use the method I described). So. If LoseWindow needs some text and also a panel (ui elements), attach both text and panel to a GameObject (LoseWindow - default NOT set active) and then when needed (when player dies) set LoseWindow active.
What should i do with that ? ...
public class HUD : $$anonymous$$onoBehaviour {
public GameObject loseWindow;
public void ShowWindow(GameObject loseWindow)
{
loseWindow.SetActive(true);
//ControlCH.Instance.State = GameState.Pause;
}
public void ShowLevelLoseWindow()
{
ShowWindow(loseWindow);
}
static private HUD _instance;
public static HUD Instance
{
get
{
return _instance;
}
}
private void Awake()
{
_instance = this;
}
public void ButtonRestart()
{
}
}
Because when I'm trying to access ShowLevelLoseWindow() method in method Die() of character script - it is not working =( ...
I see. The problem is that you are trying to access a variable from a HUD(script) in PlayerScript (right??)
If this is what you are wanting to do ([Check this][1])
you are going to want something like:
GameObject HUD = GameObject.Find("////"); //the gameobject HUD script is attached to//
HUDScript hudscript= HUD.GetComponent<//NameOfScript - I believe "HUD">();
hudscript.LoseWindow.SetActive(true);
something like this. [1]: http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html
No = (I don't think that it is a problem, because that's why I did "HUD _instance" in the script - to access anything from it and any another script. See example(That was already mentioned). The problem is that "it" can't see what to get and where its place to... That is a problem.
public void GameOver()
{
HUD.Instance.ShowLevelLoseWindow();
}
public void Destroyed()
{
GameOver();
//Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
}
Your answer
Follow this Question
Related Questions
How can i set transform.position on y ? And why i'm getting error cannot convert double to float ? 3 Answers
Displaying score from a scene to another 2 Answers
How would i make it transform player.position =target.position? 2 Answers
How can i rename a bool variable in inspector according to false/true mode ? 1 Answer