- Home /
GameObjects name As GuiText
Hi Guys
I am Trying to Display gameObjects name on screen in GameView By Gui Text But Experincing a problem Unity is sayng that the object is null which i am instanciating it Can anyone Suggets what i am doing wrong here
basicaly i want that each of my game object has its name above them as guitext or anything else and all guitext be destroyed to when the need is over like toggel
plz help
Thanks
here is my code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class JUMP_Caracter : MonoBehaviour {
string stringToEdit = "Building no";
public GameObject player ;
GameObject activeChild;
bool Show_boumdingboxes = false ;
bool Disable_scale_Script = false ;
bool Show_Obj_name = false ;
GameObject objectname ;
void OnGUI() {
GUILayout.Window(0,new Rect(10, 80, 600, 120), DoMyWindow, "Position");
}
void DoMyWindow(int windowID) {
stringToEdit = GUILayout.TextField(stringToEdit, 25);
if (GUILayout.Button("Go to Building's Position"))
{
Change_Position();
}
// Show_boumdingboxes = GUILayout.Toggle(Show_boumdingboxes, "Show Colliders") ;
Disable_scale_Script = GUILayout.Toggle(Disable_scale_Script, "Enable Gui Point to Point Scale") ;
if (GUILayout.Button("Do"))
{
Set_Scale();
}
Show_Obj_name = GUILayout.Toggle(Show_Obj_name, "Enable Game Objects Name ");
if (GUILayout.Button("Do"))
{
OBJ_Name();
}
}
// Use this for initialization
void Start () {
}
void Change_Position()
{
GameObject[] gameObjs = (GameObject[]) FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjs)
{
if(go.name.Contains("_BLD_"))
{
string building_name = go.name;
if(building_name.Contains(stringToEdit))
{
GameObject obj = GameObject.Find(building_name);
SetLayer();
activeChild.transform.position = obj.transform.position;
// activeChild.transform.position = new Vector3 (obj.transform.position.x+8,obj.transform.position.y,obj.transform.position.z);
// activeChild.transform.eulerAngles.y = obj.gameObject.transform.eulerAngles.y;
string[]name = building_name.Split(char.Parse("_"));
}
}
}
}
void SetLayer ()
{
foreach(Transform Child in player.transform)
{
if(Child.active)
activeChild = Child.transform.gameObject;
}
}
void Set_Scale()
{
if(Disable_scale_Script)
{
gameObject.AddComponent("Draw_LINE_ON_GUI_P2P_dist");
}
else
{
Destroy(GetComponent("Draw_LINE_ON_GUI_P2P_dist"));
}
}
void OBJ_Name()
{
if(Show_Obj_name)
{
GameObject[] gameObjs = (GameObject[]) FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjs)
{
objectname = GameObject.Instantiate(Resources.Load("HoverText")) as GameObject;
GUIText name = objectname.GetComponent<GUIText>();
name.text = go.name; // Change Me
objectname.transform.position = go.transform.position + new Vector3(0f, 5f, 0f); // Change the 0.05f value to some other value for desired height
}
}
else
{
// objectname.Remove();
}
}
// Update is called once per frame
void Update () {
}
}
Which object is being referenced as null? Is it every object which has the guiText above them? Could you give the exact error.
ArgumentException: The thing you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message)
Exact error and i guess the object i want to instantiate is
I think that your two GameObjects, which are activeChild and objectname might be causing you some problems. You are trying to create an instance of the object, but you haven't initialized it. So you haven't given it any value.
I initialized it at the top both of um nd as far as active child is concern its workin fine but the problem is with objectname
anyone else plz tell me wat i m doing wrong here or as it can be possible or not ??
Answer by AR_Rizvi · Feb 12, 2014 at 01:56 PM
Got It I worked With Text Mesh Nd it works perfectly
Here Is the Method OF COde I Wrote
void OBJ_Name()
{
if(Show_Obj_name)
{
GameObject[] gameObjs = (GameObject[]) FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjs)
{
if(go.GetComponent("MeshRenderer"))
{
if(go.name.Contains(ObjectNAmeDis))
{
Preobjectname =GameObject.Instantiate(objectname)as GameObject ;
// GUIText name = objectname.GetComponent<GUIText>();
if(Preobjectname.GetComponent<TextMesh>())
{
// Preobjectname.AddComponent<TextMesh>();
TextMesh name = Preobjectname.GetComponent<TextMesh>();
name.text = go.name; // Change Me
name.font = fon ;
name.fontSize = 5;
name.anchor = TextAnchor.UpperCenter ;
name.alignment = TextAlignment.Center ;
name.offsetZ = 0;
name.color = Color.red;
name.characterSize = 1;
name.richText = false;
Preobjectname.transform.position = go.transform.position;
Preobjectname.transform.eulerAngles = go.transform.eulerAngles ;
Preobjectname.transform.rotation = Quaternion.Euler(90f,25f,80f);
objs.Add(Preobjectname);
}
else
{
}
}
}
}
}
else
{
foreach(GameObject _Object in objs)
{
Destroy(_Object);
}
}
}
Can See Every Game Object's Name With it Thanks AR
Answer by Jasper-Flick · Feb 08, 2014 at 03:45 PM
The error tells you're trying to instantiate something that is null. You're instantiating something in one place, and it is Resources.Load("HoverText"). So the loading failed. Check whether the HoverText file is in your Assets/Resources folder and whether you spelled it correctly.
yup its in the folder and it spelld right wat is going wrong then
Your answer
Follow this Question
Related Questions
Problems using NGUI for in-game options sliders 0 Answers
How to make text appear when moving next to an object? 3 Answers
Unity3D Game Time 1 Answer
create a GuiSkin 2 Answers