- Home /
I am trying to print text in unity using this command 'using function OnGui () {'
But no matter where i put it the function goes red and it says the name function does not exist in the current context
using UnityEngine; using System.Collections;
public class robot : MonoBehaviour {
public float maxSpeed = 10f;
bool fasingRite = true;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move < 0 &&!fasingRite)
Flip ();
else if(move > 0 && fasingRite)
Flip ();
}
void Flip ()
{
fasingRite = !fasingRite;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by Cherno · Mar 19, 2014 at 01:57 AM
What do you mean by "print text"? If you want to make text appear on screen, you just call it from inside the OnGUI() function as detailed here:
https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnGUI.html
If you want to print to the console, you use
Debug.Log("message");
or
print("message");
Also, "function" is not used in C#, it is for UnityScript. In C#, as your script is, you call the OnGUI function like the Start and Update functions (it is typically placed after the Update function:
void OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "I am a button"))
print ("You clicked the button!");
}
Edit: Also², why do you post the same question twice?
Your answer
Follow this Question
Related Questions
Render Debug.Log as UI Text 2 Answers
WWW. Print gives a 'Message Truncated' 1 Answer
Display a block of text to the screen 0 Answers