Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dewaltamos · Jun 13, 2015 at 03:19 PM · c#guilayouttostringdice

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:

alt text

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!

dicerolledissue.png (409.6 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jun 13, 2015 at 01:47 PM 0
Share

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...

avatar image dewaltamos · Jun 14, 2015 at 12:47 AM 0
Share

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?

avatar image digzou · Jun 15, 2015 at 08:08 AM 0
Share

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. :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges