- Home /
Displaying a gameobject over gui issues HELP
Right now im trying to display a gameobject over a menu that is ran by OnGUI. I have followed many tutorials and looked up similar questions to this one but i cant seem to get it to work.
Like i said ive followed many tutorials on how to set it up so please dont simply link me to a page showing step by step how to do it. Here are my settings for the main camera that runs the gui.
Under the culling mask all of them are selected except for OverGUI Here are the settings for the other camera that will show the gameobject over the gui
And finally the object that i want to display has the layer set to OverGUI. Any help on this issue would be much appreciated, like i said ive gone through multiple tutorials and i havent had any luck on resolving the issue.
Ive had this up for about 10 days now... Still cant figure out the problem, any help would be greatly appreciated
EDIT - GUI script -
using UnityEngine;
using System.Collections;
public class GUIscript : MonoBehaviour {
public GUISkin heatlh;
public GUISkin heatlh2;
public GUISkin heatlh3;
public GUISkin xp;
public GUISkin xp2;
public GUISkin xp3;
public GUISkin greentext;
public GUISkin redtext;
public GUISkin gameovergui;
public GameObject fallingobj1;
public GameObject fallingobj2;
public GUISkin menu;
public bool ishovering = false;
public bool ishoveringweap = false;
public GUISkin inventoryfont;
private bool ininventorymenu = false;
private bool ingame = true;
private bool inleveling = false;
public int weaplevel;
public float weapdamage;
public string weaptype;
public string weapname;
public float weapattspeed;
public Transform weaplocked;
public int playerweaplevel;
public float playerweapdamage;
public string playerweaptype;
public string playerweapname;
public float playerweapattspeed;
public int selectedweaplevel;
public float selectedweapdamage;
public string selectedweaptype;
public string selectedweapname;
public float selectedweapattspeed;
public int enemylevel;
public float enemycurrenthealth;
public float enemymaxhealth;
public Transform enemylocked;
private playersetup playersetup;
private cubeenemy cubeenemy;
// Use this for initialization
void Start () {
playersetup = transform.GetComponent<playersetup> ();
}
void OnGUI(){
//This segment here is what is being shown in the picture -
if (ininventorymenu == true) {
GUI.Box(new Rect(50,50,Screen.width-100,Screen.height-100),"");
for(int x = 0;x<3;x++){
for(int y = 0;y<4;y++){
if (playersetup.Inventory[(y*4)+x].name == null){
GUI.Box(new Rect((x*Screen.width/10)+100,(y*Screen.height/6)+100,Screen.width/14,Screen.height/8),"");
}else{
GUI.DrawTexture(new Rect((x*Screen.width/10)+100,(y*Screen.height/6)+100,Screen.width/14,Screen.height/8),Resources.Load(playersetup.Inventory[(y*4)+x].name) as Texture);
}
if(Input.GetMouseButton(0) == true){
if (new Rect((x*Screen.width/10)+100,(y*Screen.height/6)+100,Screen.width/14,Screen.height/8).Contains(Event.current.mousePosition)){
selectedweapname = playersetup.Inventory[(y*4)+x].name;
selectedweaplevel = playersetup.Inventory[(y*4)+x].level;
selectedweapdamage= playersetup.Inventory[(y*4)+x].damage;
selectedweaptype = playersetup.Inventory[(y*4)+x].itemtype.name;
selectedweapattspeed = playersetup.Inventory[(y*4)+x].attackspeed;
}
}
if (selectedweapname != null){
GUI.skin = inventoryfont;
GUI.Label (new Rect (Screen.width- 550 ,Screen.height/2-300, 500,200), selectedweaptype);
GUI.Label(new Rect(Screen.width- 600,Screen.height/2-250,500,200),"Level " + selectedweaplevel + " "+ selectedweapname);
GUI.Label(new Rect(Screen.width- 600,Screen.height/2-150,500,200),"Damage: " + selectedweapdamage);
GUI.Label(new Rect(Screen.width- 600,Screen.height/2-50,500,200),"Attack Speed: " + selectedweapattspeed);
}
}
}
if(GUI.Button (new Rect(80, Screen.height-90,100,40),"Resume")){
Time.timeScale = 1;
ininventorymenu = false;
ingame = true;
}
}
//end of the segment
if(ingame == true){
if(GUI.Button (new Rect(80, Screen.height-90,100,40),"Inventory")){
//Time.timeScale = 0;
selectedweapname = playersetup.currentweapon.name;
selectedweaplevel = playersetup.currentweapon.level;
selectedweapdamage = playersetup.currentweapon.damage;
selectedweaptype = playersetup.currentweapon.itemtype.name;
selectedweapattspeed = playersetup.currentweapon.attackspeed;
ininventorymenu = true;
ingame = false;
}
GUI.Box(new Rect(20,Screen.height - 220,330,200),"");
GUI.skin = heatlh;
GUI.Label(new Rect(140, Screen.height-220,200,30),"Overall Level " + playersetup.playerlevel);
GUI.Box (new Rect(80, Screen.height-200,200,30),"");
if (playersetup.player.overhealth > 0) {
GUI.skin = heatlh2;
GUI.Box (new Rect (80, Screen.height-200, (playersetup.player.currenthealth * 200 /playersetup.player.overhealth), 30), "");
}else{
GUI.skin = gameovergui;
GUI.Label(new Rect(Screen.width/2-100,Screen.height/2,200,100),"GAME OVER");
}
GUI.skin = heatlh3;
GUI.Box (new Rect(80, Screen.height-200,200,30),"Health: " + playersetup.player.currenthealth+ "/" + playersetup.player.overhealth);
GUI.Label(new Rect(130, Screen.height-150,200,30),playersetup.currentweapon.itemtype.name +" Level: " + playersetup.currentweapon.level);
GUI.skin = xp2;
GUI.Box (new Rect(80, Screen.height-130,200,30),"");
if(playersetup.currentweapon.itemtype.currentxp > 0){
GUI.skin = xp3;
GUI.Box (new Rect (80, Screen.height-130, (playersetup.currentweapon.itemtype.currentxp * 200 /playersetup.currentweapon.itemtype.xptolevel), 30), "");
}
GUI.skin = xp;
GUI.Box (new Rect(80,Screen.height-130,200,30),"Weapon: " + playersetup.currentweapon.itemtype.currentxp+ "/" + playersetup.currentweapon.itemtype.xptolevel);
if (ishovering == true & ishoveringweap == false){
if(enemylocked){
if (enemylocked.parent.transform.GetComponent<cubeenemy>().distance < 10){
GUI.skin = menu;
GUI.Box(new Rect(Screen.width- 350,Screen.height - 220,330,200),"");
GUI.Label(new Rect(Screen.width- 210,Screen.height - 200,100,200),"Level " + enemylocked.parent.GetComponent<cubeenemy>().level + " Cube");
GUI.skin = heatlh;
GUI.Box (new Rect(Screen.width- 270,Screen.height - 180,200,30),"");
GUI.skin = heatlh2;
GUI.Box (new Rect (Screen.width- 270 ,Screen.height - 180, ( enemylocked.parent.GetComponent<cubeenemy>().currenthealth * 200 / enemylocked.parent.GetComponent<cubeenemy>().maxhealth), 30), "");
GUI.skin = heatlh3;
GUI.Box (new Rect(Screen.width- 270,Screen.height - 180,200,30),"Health: " +enemylocked.parent.GetComponent<cubeenemy>().currenthealth+ "/" + enemylocked.parent.GetComponent<cubeenemy>().maxhealth);
}else{
ishovering = false;
}
}
}else if(ishovering == false & ishoveringweap == true){
if(weaplocked){
if (weaplocked.GetComponent<weaponhover>().distance < 2){
GUI.skin = menu;
GUI.Box(new Rect(Screen.width- 350,Screen.height - 220,330,200),"");
GUI.Label (new Rect (Screen.width- 310 ,Screen.height - 220, 200,200), weaptype);
GUI.Label(new Rect(Screen.width- 330,Screen.height - 200,200,200),"Level " + weaplevel+ " " + weapname);
GUI.Label (new Rect (Screen.width- 150 ,Screen.height - 220, 200,200), "Current Weapon");
GUI.Label(new Rect(Screen.width- 180,Screen.height - 200,200,200),"Level " + playerweaplevel + " "+ playerweapname);
if (GUI.Button(new Rect (Screen.width- 300 ,Screen.height - 100, 80,30),"Equip")){
//do{
//
//}while(transform.GetComponent<
}
if(weapdamage > playerweapdamage){
GUI.skin = greentext;
GUI.Label (new Rect (Screen.width- 330 ,Screen.height - 170, 200,200), "Damage: " + weapdamage);
GUI.skin = redtext;
GUI.Label (new Rect (Screen.width- 150 ,Screen.height - 170, 200,200), "Damage: " + playerweapdamage);
}else{
GUI.skin = redtext;
GUI.Label (new Rect (Screen.width- 330 ,Screen.height - 170, 200,200), "Damage: " + weapdamage);
GUI.skin = greentext;
GUI.Label (new Rect (Screen.width- 150 ,Screen.height - 170, 200,200), "Damage: " + playerweapdamage);
}
if(weapattspeed > playerweapattspeed){
GUI.skin = greentext;
GUI.Label (new Rect (Screen.width- 330 ,Screen.height - 150, 200,200), "Attack Speed: " + weapattspeed);
GUI.skin = redtext;
GUI.Label (new Rect (Screen.width- 150 ,Screen.height - 150, 200,200), "Attack Speed: " + playerweapattspeed);
}else{
GUI.skin = redtext;
GUI.Label (new Rect (Screen.width- 330 ,Screen.height - 150, 200,200), "Attack Speed: " + weapattspeed);
GUI.skin = greentext;
GUI.Label (new Rect (Screen.width- 150 ,Screen.height - 150, 200,200), "Attack Speed: " + playerweapattspeed);
}
}else{
ishoveringweap = false;
}
}
}
if(inleveling==true){
}
}
}
public void gameover(){
Instantiate (fallingobj1 ,new Vector3(Camera.main.transform.position.x-4.5f,6.5f,-3), Quaternion.identity);
Instantiate (fallingobj2 ,new Vector3(Camera.main.transform.position.x-3,Camera.main.transform.position.y-2,-3), Quaternion.identity);
}
}
I wasn't sure if you wanted specifically what was being displayed in the picture or the entire script so i just pasted it all.
Here is the current result im getting
as you can see the gameobject is being displayed behind the gui
Hi, I'm a newbie, but perhaps you can try to move the GUI farther away from the camera relative to the gameObject. Typically, z-xais. This may be a poor suggestion, but like I said I'm a newbie.
Well since the gui isnt an actual gameobject, it doesnt have an assigned z-axis value because it has no depth, thats why i have two cameras set up so that one camera will overlay the other, which contains the gui. Thanks for the tip though.
Thanks for setting me straight. I'm still learning all of this. Have you looked into NGUI?
NGUI is already available in the Asset Store. Check Link .
As for your actual issue with the GUI you have at the moment, would you be able to share the current code you have setup for it? By the looks of the screenshot (looks can be deceiving however) it's like an additional overlay is blocking your character with no 'GUI' reasons to be there.
I tried putting a game object over my GUI for my inventory (so I could have a 3d spinning effect) and never got it to work, despite many tutorials clai$$anonymous$$g it was possible. I'm just starting with Unity 4 so maybe it can be done now, but my solution was something like what Toggy said: mimic the GUI with game objects (3d text, etc.). Alternatively, you could shape the GUI so it only displays around the outside of the screen.
Answer by oddgoo · Jan 17, 2014 at 07:37 AM
the GUI methods are always displayed at the end. This means you cannot draw the game world over it or display a gameobject on top.
One convoluted way would be to use a camera with "Don't clear" as the clear flags, set it to render into a texture (Unity Pro only) and a script that gets the pixels of that RenderTexture and displays them on the OnGUI thread as another texture.
Well I undestand that but then there is no way to render the 3d objects over the gui windows with unity free??? :C I already tried with 2 cameras and the other answers but the objet is in the back all the time. Thanks
Answer by JeffreyD · Jan 18, 2014 at 05:35 AM
I just read this thread link from aeroson. http://answers.unity3d.com/questions/202670/gameobject-on-top-of-gui.html It works great. So, he should get the credit. Two (or more) cameras, different depths. I put the GOs (Game Objects) on a second camera at a higher depth and set the Don't clear" on the second camera. I was able to see GOs on top of the NGUI layout I had created. Very cool. You can also watch the cameras training in the LIVE Training session (Archives) on the Unity site. http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/cameras They show how to create a second camera in a 3D setting, but the concept still applies. Hope this helps.
P.S. I'm not yet sure how the two camera setup works for scaling on mobile devices. etc... I still have problem with that using one camera, but that's a topic for a separate thread.
Answer by aeroson · Jan 17, 2014 at 02:23 PM
This worked for me: http://answers.unity3d.com/questions/202670/gameobject-on-top-of-gui.html, but it will render GUI behind everything.
GUI is rendered above everything. You could theoretically change its shader to not do so. Yet i didn't find how to.
You could use some other GUI system such as NGUI, there is a free version of it on asset store. You can use your own shaders here.
Wait for Unity to release ther new GUI system similar to NGUI
Your answer
Follow this Question
Related Questions
Procedurally generated uGUI elements render under world space objects 1 Answer
[CLOSED] Render priority of gameObjects 0 Answers
Showing a layer with GUI Box(Behind layer) on click of game Object 1 Answer
Version 5: OnGui() over UI Objects, and vice versa 0 Answers
Displaying gameobjects over gui 0 Answers