- Home /
3 Lives GUI
I have 2 separate scripts, one with the value of how many lives the player has left and spawn the player when they trigger it, the other is attached to the GUI hearts I have in my canvas to remove hearts when the player dies. My problem is this, I cant seem to get the GUI script to read how many lives I have left, when the integer goes down to 2 the hearts stay 3. I do not know whether this is a problem with my scripts, but here they are just in case their the cause:
PlayerRespawn
public int LevelNumber;
public GameObject player;
public Transform playerSpawnPoint;
public int LIVES = 3;
void OnCollisionEnter2D(Collision2D target)
{
if (target.gameObject.tag == "Player")
LIVES -= 1;
{
switch (LIVES)
{
case 3:
target.transform.position = playerSpawnPoint.position;
break;
case 2:
target.transform.position = playerSpawnPoint.position;
break;
case 1:
target.transform.position = playerSpawnPoint.position;
break;
case 0:
Application.LoadLevel(LevelNumber);
break;
}
}
}
}
HeartControl
public int LIVES;
public Texture2D Heartlife1;
public Texture2D Heartlife2;
public Texture2D Heartlife3;
void Update ()
{
Boundary.PlayerRespawn.LIVES ();
switch(LIVES)
{
case 3:
guiTexture.texture = Heartlife3;
break;
case 2:
guiTexture.texture = Heartlife2;
break;
case 1:
guiTexture.texture = Heartlife1;
break;
}
}
}
Answer by SkaredCreations · Dec 24, 2014 at 01:12 PM
Why have you LIVES in both scripts? It should be only in one of them, and the other must read the value from the script that has it. For example leave it only in PlayerRespawn and then in HeartControl removes it and in your "switch" statement use Boundary.PlayerRespawn.LIVES (without the parenthesis, because they mean you're calling a function and not accessing to a property, so remove the line 11)
Changed the HeartControl to :
void Update ()
{
switch (Boundary.PlayerRespawn.LIVES)
{
and get this messege:
Assets/Scripts/HeartControl.cs(16,25): error CS0103: The name `Boundary' does not exist in the current context.
Am I referencing the Boundary right, it's a game object with just a collider on it.
But I don't see any Boundary declared or assigned in the HeartControl script, so where are you setting it?
I am new to codeing, when you say declare Boundary am I supposed to have a public Boundary; at the top?
If you have this code in PlayerRespawn:
public static PlayerRespawn instance;
void Awake () {
instance = this;
}
Then in HeartControl you can do:
void Update ()
{
if (PlayerRespawn.instance != null)
{
switch(PlayerRespawn.instance.LIVES)
{
case 3:
guiTexture.texture = Heartlife3;
break;
case 2:
guiTexture.texture = Heartlife2;
break;
case 1:
guiTexture.texture = Heartlife1;
break;
}
}
}
Thank you, this does now make my scripts talk to each other :D. I seem to have run into a brick wall with unity 4.6, I have used the up to date HUDcanvas and I think I need to change the guiTexture.texture to something else, the GameObject that the script is attached to has a Image (Script) which displays the 3 hearts, I cant find online how to link that in place of the guiTexture bit.
Your answer
Follow this Question
Related Questions
How to increase the life of the player? 2 Answers
Life Bar controlled 1 Answer
Regain health on GUI 2 Answers
Life Player loads another level 1 Answer