- Home /
Display GUI menu after last object left the screen
Hi guys
I am creating one game where my camera is still and some objects pass from front of the camera from right to left.
I am trying to display the GUI menu after last object left the screen. But don't know how to do that.
This is my game master script where I am instantiating prefabs
#pragma strict
var respawn1:GameObject;
var randomcars:int;
var i:int;
function Start () {
randomcars = Random.Range(10,20);
for (var i : int = 10;i < randomcars; i++) {
Instantiate (respawn1, Vector3(i * 5, 0, 0), Quaternion.identity);
}
}
function Update (){
}
2nd is my obejct script , just to move object from right to left of screen
#pragma strict
var moveSpeed= -5;
function Start () {
}
function Update () {
transform.position.x += moveSpeed * Time.deltaTime;
if (transform.position.x <-12){
Destroy(gameObject);
}
}
Answer by robertbu · Sep 28, 2014 at 06:20 AM
I see you are destroying your objects when they are off the screen. If you tag your game objects with the same, unique tag, you can integrate the following into your code:
var showGUI = false;
function Update() {
showGUI = (GameObject.FindWithTag("TagOnObjects") == null);
}
function OnGUI() {
if (showGUI) {
// GUI code goes here
}
}
Hi Robertbu, sorry but I didn't get it. I am noob. So here is the thing. I have one obejct, its a prefab and it has tag "cars". So I am instantiating that prefab from right of the screen (in random numbers). So lets say 4 prefabs generated and they all have "Player" tag. And now when all 4 prefabs go out of screen (to left) I am trying to pop up GUI. So where should the above code will go? it can't go to that prefab object right? because its getting destroyed. So do you mean this will go to the game master script?
And I couldn't understand the logic
showGUI = (GameObject.FindWithTag("TagOnObjects") == null);
If you please explain me, that would be great
Thanks
Put it on a master script.
GameObject.FindWithTag returns null if no objects are found. So if this is == null then you set showGUI to true. And your GUI shows up.
One more advanced way to do this is to have your spawn script run as a coroutine. Once all of the spawning is complete you then start checking if any objects remain on screen. This approach allows you to have lulls in your spawn script without problems.
As usual, both @Bored$$anonymous$$ormon's comments are correct. Put the script on any game object that is not getting destroyed. And reiterating, this line:
showGUI = (GameObject.FindWithTag("cars") == null);
sets 'showGUI' to true when no object are found with the given tag.
Thank you very much Robertbu & Bored$$anonymous$$ormon. It is working now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163932im_/https://answers.unity.com/themes/thub/images/avi.jpg)