- Home /
Simple letters game
Letters appear on the screen one by one, at a rate of one per second.
Instructions: “Letter will appear on the screen one at a time. Every time you see the letter 'A', click the letter with your mouse. When letters other than 'A' appear, do NOT do click or tap the letter”.
Scoring: Give one point if there is an error (an error is a click on a wrong letter or a failure to click on letter A).
I am completely new to Unity. Can someone help me as to how should I start building this?
Thanks.
Answer by aldonaletto · Jul 09, 2013 at 03:50 AM
TextMesh is a component, thus it only exists as part of a GameObject. When you create a 3D text with the Hierarchy Create button, a game object is created with a TextMesh component included.
In your case, a Raycast seems to be the more indicated: attach the script to the camera and verify what's being clicked. The code could be something like this:
var score = 0; // the score increases each time an A is clicked
function Update(){
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
// calculate a ray passing through the mouse pointer:
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){ // if something hit...
var txtMsh: TextMesh = hit.transform.GetComponent(TextMesh);
// and if it's a TextMesh "A"...
if (txtMsh && txtMsh.text == "A"){
score++; // increment the score
}
}
}
}
You must add a box collider to the 3D text object - due to some weird reason, mesh colliders in 3D text objects aren't detected by Raycast.
Notice that the logic above seems opposed to what your question says: the score is incremented when an A is clicked. If you want to increase the score when an error occurs, change the logic:
...
if (Physics.Raycast(ray, hit)){ // if something hit...
var txtMsh: TextMesh = hit.transform.GetComponent(TextMesh);
// if it's not a TextMesh, or if it's not an A...
if (!txtMsh || txtMsh.text != "A"){
score++; // increment the score
}
}
}
}
Answer by Em3rgency · Jul 08, 2013 at 08:46 PM
Have gone through the Introduction and Scripting tutorials. How do I register the input when a mouse is clicked on a letter? Also, I used Text$$anonymous$$esh to display letters. Is Text$$anonymous$$esh called a GameObject?
No, it's a Component. Components are attached to GameObjects.
Your answer
Follow this Question
Related Questions
String array not working 1 Answer
How to convert a string to int array in Unity C# 1 Answer
convert string to list of lists 1 Answer
Array of Array 1 Answer
Is it possible to split a string without specifying a separator? 1 Answer