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 /
avatar image
0
Question by sinameraji · Nov 14, 2016 at 11:40 AM · uiprefabfunction

Prefab: Printing single letters of a word in UI Panel (one public variable different values)

Hello.

I'm new to Unity and trying to make a basic Lingo game (a word missing 1 letter, so the user will have to select the right letter from the 2 options). So I've made a prefab for the yellow panel and its text.

Here's my problem: Suppose the word is "apple" and I want every yellow panel to hold 1 char. How can I achieve that with prefab? This is my code and what I get right now is A A A A A (first letter in "apple" which is "A" is dequeued and shown in every panel...). What should I do?
Thanks!

alt text

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 using System.Collections.Generic;
 
 public class WordStack : MonoBehaviour {
     string word;
     public Text letter;
 
     void Start(){
         word = "apple";
         Queue<string> lettersQueue = new Queue<string>();
         foreach (char element in word){
             lettersQueue.Enqueue (element.ToString().ToUpper());
         }
         letter.text = lettersQueue.Dequeue();
     }
 
      void Update(){
         
 
     }
 }
 


 


screen-shot-2016-11-14-at-62515-pm.png (24.7 kB)
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
0

Answer by ESchrauwen · Nov 14, 2016 at 12:30 PM

I can't really tell how this code relates to the rest of your project, but if this is the script that is attached to your prefab, it makes sense that it only ever gives you "A", because after Enqueueing all the letters, you only Dequeue once, which always returns the first item in your Queue.

Here's an idea; Create a script on your prefab which has a public variable called "letter". In the Start function, all you do is display that letter.

Generating the prefabs, each with its own letter, is not a job that should be done by each indivudual letter-prefab. Rather, you could make another object with a script on it that instantiates these prefabs.

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

Answer by sinameraji · Nov 14, 2016 at 04:40 PM

I can't really tell how this code relates to the rest of your project

Could you please elaborate a bit? Honestly I'm quite puzzled about the flow of a typical program in Unity.

I'd really appreciate it if you could help me get the bigger picture of how this should be done ideally. Thank you

Comment
Add comment · Show 3 · 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 ESchrauwen · Nov 14, 2016 at 04:55 PM 1
Share

If you attach a script to a prefab, that script will be running for every instance you create of this prefab. So in your case, you have 5 letter-prefabs, which means you have 5 WordStack scripts - one for each prefab.

I don't think that that is what you want. I think you want 1 object that takes care of creating prefabs (one for each letter in a given word). The prefabs themselves don't really have to do anything, other than display the letter they are assigned.

avatar image sinameraji ESchrauwen · Nov 17, 2016 at 05:29 AM 0
Share

Thank you! One more question. How can the prefabInstantiator object create different prefabs? (different letter for each prefab) I can't find a way to pass variable to the prefab and make them different

avatar image ESchrauwen sinameraji · Nov 17, 2016 at 08:32 AM 0
Share

The prefab would also has a script on it, and in that script you can add a public variable to hold the letter value.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why does this prefab trigger all instances to action? C# 1 Answer

Use Instantiated clone in the next function in same class 1 Answer

Using raycast to enable canvas that is linked to a prefabs gameobject 0 Answers

Buttons on menu prefab showing on scene view but not in game 0 Answers

Canvas Button OnClick() function arguments 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