- Home /
Dice roll - ToString text placement using GUILayout
I have been using Unity 5 for a couple weeks making a Craps game as a hobby. I have been making decent progress but am stumped on this. I am trying to have the Dice values displayed in a position and formatting of my choosing.
The Dice values are in the top-left, overlapped. Pretty ugly/useless there. I believe this screenshot explains the issue:
Here is the C# script I am using:
using UnityEngine;
using System.Collections;
public class DisplayCurrentDieOneValue : MonoBehaviour
{
public LayerMask dieOneValueColliderLayer = -1;
public int currentDieOneValue = 1;
// Update is called once per frame
void Update ()
{
RaycastHit hit;
if (Physics.Raycast (transform.position, Vector3.up, out hit, Mathf.Infinity, dieOneValueColliderLayer))
{
currentDieOneValue = hit.collider.GetComponent<DieOneValue> ().value;
}
}
void OnGUI()
{
GUILayout.Label (currentDieOneValue.ToString());
}
}
The script and principle are functioning correctly, I just don't know how to place and format these values in a better way.
I have tried these variations, but Unity will not compile:
GUILayout.Label (Rect(100,100,100,50),currentDieOneValue.ToString());
GUILayout.Label (Rect new(100,100,100,50),currentDieOneValue.ToString());
GUILayout.Box (Rect(100,100,100,50),currentDieOneValue.ToString());
GUILayout.Box (Rect new(100,100,100,50),currentDieOneValue.ToString());
Am I close or on the right track, or far from it? Any help would be appreciated!
when you get compile errors, it's a good idea to post them here.
the syntax for a new Rect
is:
new Rect(left,top,width,height)
which you'll find in the docs here
however, you might consider using something other than OnGUI()
- the new UI is infinitely better...
Thanks for the response gjf. I have looked at using the new UI, which looks like a much cleaner and efficient method.
Basically, I want to use the rolled dice value in the new UI. I watched a tutorial that is very close to achieving this; they are having a "score" displayed, whereas I am trying to display the value of the die rolled.
This is the tutorial: UI Text tutorial (relevant starting at 2:49)
I set my UI up the same way, but it is not updating the value.
This is the C# script on my GameObject (tagged "Die1"):
using UnityEngine;
using System.Collections;
public class DisplayCurrentDieOneValue : $$anonymous$$onoBehaviour
{
public Layer$$anonymous$$ask dieOneValueColliderLayer = -1;
public int currentDieOneValue = 1;
// Update is called once per frame
void Update ()
{
RaycastHit hit;
if (Physics.Raycast (transform.position, Vector3.up, out hit, $$anonymous$$athf.Infinity, dieOneValueColliderLayer))
{
currentDieOneValue = hit.collider.GetComponent<DieOneValue> ().value;
}
}
void OnGUI()
{
GUILayout.Label (currentDieOneValue.ToString());
}
}
This is the script that is on the UI Text object:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace CompleteProject
{
public class Die1Value : $$anonymous$$onoBehaviour
{
public static int currentDieOneValue; // This is the integar that represents what number was rolled on the Die.
Text dievalue; // Reference to the UI Text component that this script is attached to.
void Awake ()
{
// Set up the reference.
dievalue = GetComponent <Text> ();
}
void Update ()
{
// Set the displayed text to be the work "Die 1 rolled: " followed by the rolled number by the Die.
dievalue.text = "Die 1 rolled: " + currentDieOneValue;
}
}
}
So the value is updated when this is used...
void OnGUI()
{
GUILayout.Label (currentDieOneValue.ToString());
}
...but when I try to use this data in the new UI, it doesn't seem to update the UI text with the rolled die value.
Because I am new, I have a feeling I am missing something small, but just don't know what to look for. Any ideas?
In your DisplayCurrentDieOneValue class, change public int currentDieOneValue = 1;
to public static int currentDieOneValue = 1;
I would strongly suggest you to move on with new uGUI. $$anonymous$$uch easy to and clean to use. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dynamic GUI Columns in GUI Area? (C#) 1 Answer
adding a force to a dice and find out which face collided with the ground 2 Answers
Enum to int issue 1 Answer