- Home /
Question by
Harryliu · Apr 29, 2016 at 04:02 AM ·
javascriptscripting problemuiraycasttext
UI Text showing weird string value?
I have this script that essentially selects an object using Raycast and displays the name of it via Text UI. Everything works fine, but the text that appears on screen when the object is selected is not what I want. This is what I see:
And this is the string value on the script that is being referenced:
And here is the script:
#pragma strict
import UnityEngine.UI;
var cam : Transform;
var layerMask : LayerMask;
var mat : Material;
var selectedObject : GameObject;
var Text : Text;
var objText : String;
private var showObj : boolean;
function Start () {
}
function Update () {
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if(Physics.Raycast(cam.transform.position, cam.forward, hit, 100, layerMask))
{
selectedObject = hit.transform.gameObject;
objText = selectedObject.GetComponent(Item).objName;
SelectObject(selectedObject);
Debug.Log("You hit " + objText);
showObj = true;
ShowSelectedObject(selectedObject);
}
else
{
ClearSelection();
}
}
function ShowSelectedObject(sobj : GameObject)
{
if(showObj == true)
{
Text.text = ("" + objText.ToString);
}
}
function SelectObject(obj : GameObject)
{
if(selectedObject != null)
{
if(obj == selectedObject)
{
return;
}
ClearSelection();
}
selectedObject = obj;
}
function ClearSelection()
{
Text.text = null;
selectedObject = null;
showObj = false;
objText = null;
}
Little help here? thanks
glitch.png
(66.3 kB)
glitch-2.png
(16.0 kB)
Comment
Best Answer
Answer by Taorcb · Apr 29, 2016 at 04:08 AM
Try changing Text.text = (" + objText.ToString);
to Text.text = objText;
Your answer
Follow this Question
Related Questions
Display the same string in multiple text boxes 1 Answer
Pause Menu Issue (Black Screen) 0 Answers
Hiding the part of a UI text behind a 3D object 1 Answer
Text wont change to correct value 1 Answer
TextField text component not updating 2 Answers