Hello,
I’m new to Unity and have been working for hours on what I believe to be a very simple problem. I can’t seem to find an object’s transform.y from outside of that object. I’m trying to have a button be displayed when my player’s lives reach 0. While I can get the button to display I also wanted this button to be dependent on where the ball is in the game (the game is a breakout clone). When the player’s lives reach 0 and the ball has fallen off the screen I want a “Play Again” button to pop up. I can’t seem to get my GUI to reference the ball object’s transform.y. I’ve tried quite a few functions but to no avail. I eventually end up with an error msg or I have essentially nothing happening.
This code isn’t my best attempt at this but after about 2 hours of failing I seem to be going backwards. I know this isn’t the correct way to do this (I shouldn’t be using GameObject.Find every frame for example and probably shouldn’t be searching in OnGUI() ) but this was my last attempt before bed. Can anyone point me in the right direction and help me out here? I really appreciate you taking the time!
var hudLives :String;
var ballPos;
function Update ()
{
hudLives = scr_Ball.lives.ToString();
guiText.text = "Lives: " + hudLives;
}
function OnGUI()
{
var ballPos = GameObject.Find("pf_ball");
if ((scr_Ball.lives == 0)// (ballPos.transform.y < -10))
{
if (GUI.Button(Rect(Screen.width/2, Screen.height/2-50, 100, 50), "Play Again"))
{
scr_Ball.lives = 4;
}
}
}
Thanks again.