Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by xMEH · Jun 13, 2017 at 08:09 PM · randomgenerator

How can i do this for my Unity App?

I'd like to create an app on unity which has just one main screen where you tap a button and it gives you random facts, i can write the facts and stuff but how do I make the button which switches between the facts and how do I put the facts in a queue or something? Please Help, Thanks! (C Sharp)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jawchewa · Jun 13, 2017 at 09:03 PM

I would probably create a script that looks something like this:

 public class RandomFact : MonoBehaviour {
     public Text text;
     public List<string> facts;
     public void Click()
     {
         text.text = facts[Random.Range(0, facts.Count)];
     }
 }

Create a canvas with a button and a text object, and attach this script to the button. Then, in the editor, add this new click event to the OnClick event of the button. You'll also need to attach the text object from the canvas to the textfield in the script. Then, you should easily be able to populate the Facts list with all of the facts that you want.

Comment
Add comment · Show 5 · Share
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 xMEH · Jul 26, 2017 at 02:36 PM 0
Share

Thanks for giving your time to help me. I will definitely try this out!

avatar image xMEH · Jul 26, 2017 at 02:43 PM 0
Share

Also, what is the editor and how do i access it? Which one is the text field in the script and how do I put it in? Do I write the fact list in the script or in the canvas? Sorry I am a beginner to Unity. Thanks

avatar image xMEH · Jul 26, 2017 at 02:44 PM 0
Share

@Jawchewa .

avatar image Jawchewa xMEH · Jul 26, 2017 at 03:30 PM 0
Share

Oh, by Editor, I just meant to set it in the Inspector panel inside of Unity. I would look at this page of documentation:

https://docs.unity3d.com/$$anonymous$$anual/script-Button.html

Basically, you need to go through Unity, and Create a new GameObject with a button. You can do this by going to the top toolbar and hitting GameObject->UI->Button. Also do the same thing for Creating a text GameObject by going GameObject->UI->Text. Then, attach the script that I wrote to the button, and drag the Text GameObject into the text field for that component. You can also populate the list of strings with all of the facts that that you want in the "facts" section on the RandomFact Script.

Then, to hook up the button, click the + button on the OnClick area of the button, and set the Object to be the button game object. Then, you will have to find the Click Event from the Random Fact script.

Reading through it, I don't know if this description helps or if it just makes it more complicated. Let me know if you need any other help.

avatar image xMEH Jawchewa · Aug 07, 2017 at 04:16 PM 0
Share

Hi, I've just started to create the app. I've written your script into monodevelop and a lot of the word are 'incorrect' (underlined in red), @Jawchewa . Sorry about the hassle, I'm a noob.

avatar image
0

Answer by xMEH · Jul 26, 2017 at 03:37 PM

Thanks a lot for the help! Just one more question sorry, do I write the random facts in the actual script or in the empty game object? @Jawchewa

Comment
Add comment · Show 5 · Share
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 Jawchewa · Jul 26, 2017 at 04:02 PM 0
Share

Inside the inspector in Unity for the RandomFacts Script, there should be an area for "Facts" from where it was defined in the code. Inside this area, you should be able to add new strings that contains each of the facts.

avatar image xMEH · Aug 10, 2017 at 02:14 PM 0
Share

@Jawchewa I fixed the script. I have attached the script to the button etc but for the on click event what do I put? Also, for the random facts script there are lots of 'elements', what do I type in there? Thanks

avatar image Jawchewa xMEH · Aug 10, 2017 at 06:16 PM 0
Share

Drag the Random Fact Script into the field for the object. Then, In the function, Navigate to RandomFact->Click() and use that.

I'm not quite sure what you are referring to with elements. If you are just referring to the textboxes underneath the Facts list, then that is where you would type your Random Facts.

avatar image xMEH Jawchewa · Aug 11, 2017 at 10:03 AM 0
Share

Thanks for the quick reply, I will try this out

Show more comments

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

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I create a random fixture generator. 0 Answers

Random Generator Instantiate Tiles C# = PLEASE HELP = 0 Answers

(C#) Problem With Random Number Generator 1 Answer

How can you do borderlands style random weapons and stat generation? 2 Answers

Choose something randomly based on its grouping 0 Answers


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