How to change content of unity text? - user-interface

I create an application on unity3d. I use Unity 5.0.2f1 (64-bit). I create a new project and add an empty gameobject. I click gameobject and add a gui text from inspector. My gui text is below :
I want to change the contenf of this text at each frame. I use below code but it doesn't change it.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
private int count = 0;
public GUIText guiText;
// Use this for initialization
void Start () {
guiText.text = "Counter";
}
}
What should I do? I see asddffbhfbehfbhj every time.

Attach this script to your gameobject(the same game object that guitext is attached to)then assign the guitext component of your game object to your script via inspector(drag you guitext component and drop it in guitext variable that you see within your script component in inspector)

Related

Telerik UI For WinForms RadMessageBox font size

Is there a way to increase the default font size of the RadMessageBox for the whole application without having to create a custom theme?
If not, how to just increase the font on the default theme in Visual Style Builder? I tried just increasing the label and button font sizes, but the style builder reset the whole form that hosts the message box to look very plain and cutting the text of the label (see attached screenshot).
Thank you.
It is possible to customize the font for all RadMessageBoxes within an application.
Implement a method ('SetTelerikControlStyles') which is called before any form and/or RadMessageBox is created. You only need to call this method once!
Add the following lines of code in the created method of step 1:
RadMessageBox.Instance.FormElement.TitleBar.Font = new Font("Calibri", 25f);
RadMessageBox.Instance.Controls["radLabel1"].Font = new Font("Calibri", 50f, FontStyle.Regular);
The FormElement.TitleBar.Font, as the name already reveals, is the title bar of the RadMessageBox.
Telerik is using dynamic named controls, so in this case radLabel1 represents the RadMessageBox text area.
Complete Program.cs sample:
using System;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls;
namespace WindowsFormsApplication
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SetTelerikControlStyles();
Application.Run(new Form1());
}
private static void SetTelerikControlStyles()
{
RadMessageBox.Instance.FormElement.TitleBar.Font = new Font("Calibri", 25f);
// I added this additional check for safety, if Telerik modifies the name of the control.
if (RadMessageBox.Instance.Controls.ContainsKey("radLabel1"))
{
RadMessageBox.Instance.Controls["radLabel1"].Font = new Font("Calibri", 50f, FontStyle.Regular);
}
}
}
}

Unity Slider doesn't apply changes automatically

I've created custom Editor with Slider:
[CustomEditor(typeof(CylindricalCamera))]
public class CylindricalCameraEditor : Editor
{
public override void OnInspectorGUI()
{
CylindricalCamera camera = (CylindricalCamera)target;
camera._nearClipPlane = EditorGUILayout.Slider(camera._nearClipPlane, 0, 10);
In CylindricalCamera I am drawing a custom gizmo:
public class CylindricalCamera : MonoBehaviour
{
..
void OnDrawGizmos()
{
...
I want a gizmo in CylindricalCamera to be repainted every time I drag the slider. But in fact I have not only to drag the slider, but also to select and press Enter on the textbox near the Slider:
Custom slider
How could I force Slider to apply changes automatically, without pressing Enter?
I am using Unity 5.3.5 f1
Solved:
OnInspectorGUI()
{
...
if (GUI.changed) EditorUtility.SetDirty(target);
}

Unity 5.3 dropdown menu

I am trying to set the text of my dropdown menu options by scripting but I'm geting this error
NullReferenceException: Object reference not set to an instance of an object
My code:
function Start () {
var dropdown = GameObject.Find("Dropdown");
dropdown.options.Clear();
dropdown.options.Add(new dropdown.OptionData("tile1"));
}
This is my first project in Unity.
It's possible that Dropdown does not exist yet in the scene when Start() is called. Perhaps consider adding a public field instead of using GameObject.Find():
public GameObject dropdown;
void Start()
{
//... your code here
}
Then in the scene, drag your "Dropdown" object onto the object this script is on.

How to add a Animator Controller to a character at runtime?

I have an Animator controller at "Assets/Resources/System/PLController". I have to add it at runtime using a script. How to accomplish this. Using Unity 5 (5.3.0f4)
PLController = (Animator Controller)
First attach an Animator component to that GameObject like
Declare an Animator variable.
Assign variable through GetComponent
Assign RuntimeAnimatorController to its runtimeAnimatorController attribute.
Like,
Animator PLAnimator;
void Start ()
{
PLAnimator = GetComponent<Animator> ();
PLAnimator.runtimeAnimatorController = Resources.Load ("Assets/Resources/System/PLController") as RuntimeAnimatorController;
}

Proper use of Unity 4.6 MoveTowards

I have been working on this for several days without success. I made a sprite animation of a bird flapping it's wings. I want to make the bird fly to a specific spot in a tree. The tree is an image, which has a child image with the animation attached. The animation works fine, but I want to move it to a branch on the tree. The class listed below is attached to the child image, but the bird doesn't move toward the branch.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FlyBird : MonoBehaviour {
public Transform target; // Target is empty game object defined in the inspector
public float MoveSpeed; // Set to 10 in the inspector
// Script is attached to an Image object and the child of the image is a sprite animation
void FixedUpdate()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed * Time.deltaTime);
Debug.Log(transform.position.x);
}
}
I don't know what your current behaviour is, but a possibility is that you're moving on FixedUpdate(), so you should use Time.fixedDeltaTime instead of Time.deltaTime
You're using an UI Image, if I got correctly after your edit.
For this type of object, you should use its RectTransform instead of Transform.
Try this:
rectTransform.position = Vector2.MoveTowards(rectTransform.position, target.rectTransform.position, MoveSpeed * Time.deltaTime);
Supposing you can get rectTransform from target object, of course. Another way could be to manipulate rectTransform.anchoredPosition.
Check more information about this type of Rect Transform here: http://docs.unity3d.com/460/Documentation/ScriptReference/RectTransform.html

Resources