- Home /
Multiplying GUIs
I have a GUITexture which gets instantiated once in a single script, but in-game exists in two places. Its role is to indicate where the game objective is located by having a script draw it wherever that objective is. One icon sits at the place where it is intended to be and second one somewhere far away outside the map. It's behind my back and moves relatively to me:
Cameras I have are:
Perspective Main Camera
Orthographic Top View Camera, which is set to not render the layer that the scripted object is in, nor the gui element itself.
After disabling the topView camera, nothing changes.
Destroying the object destroys the copy. The copy is not visible in Hierarchy.
See code below. After getting rid of the code in the update nothing changes.
#pragma strict
public var objectiveIcon : GUITexture;
public var objectiveDescription : GUIText;
private var icon : GUITexture;
private var description : GUIText;
private var screenAdjustmentInYAxis : float;
function Start ()
{
icon = Instantiate(objectiveIcon);
description = Instantiate(objectiveDescription);
var height : float = Screen.height;
screenAdjustmentInYAxis = (-0.5f/(2f*(height/300f)));
}
function Update ()
{
icon.transform.position = Camera.mainCamera.WorldToViewportPoint(this.transform.position);
description.transform.position = Camera.mainCamera.WorldToViewportPoint(this.transform.position);
icon.transform.position.x = Mathf.Clamp01(icon.transform.position.x);
icon.transform.position.y = Mathf.Clamp(icon.transform.position.y, screenAdjustmentInYAxis, (1 + screenAdjustmentInYAxis));
icon.pixelInset.y = 40;
description.pixelOffset.x = description.text.Length*-3.3;
description.pixelOffset.y = 40;
Debug.Log("x pos: " + Camera.mainCamera.ViewportToScreenPoint(icon.transform.position).x);
Debug.Log("y pos: " + Camera.mainCamera.ViewportToScreenPoint(icon.transform.position).y);
Debug.Log("z pos: " + Camera.mainCamera.ViewportToScreenPoint(icon.transform.position).z);
}
function OnDestroy ()
{
Destroy(icon);
Destroy(description);
}
Your answer
Follow this Question
Related Questions
Instantiated GuiTextures not showing 1 Answer
Instantiating guiText / guiTexture 1 Answer
C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer
Dynamically instantiated game objects not showing in main camera (with background camera) 3 Answers
GUITextures not lining up correctly 2 Answers