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 TheLastBert · Oct 23, 2020 at 08:46 PM · uiprefab

Prefabs that have types other than game object,Text prefabs that isn't a GameObject

I am trying to follow the CatLikeCoding tutorial on hex maps link text

Fairly early on they label all of the cells by creating a GameObject with a text component, which they then turn in to a prefab. I do this in the usual way (dragging the GameObject to the prefabs folder). But when they come to use the prefab in code, it is used like this:

 public class HexGrid : MonoBehaviour
 {
 
     public int width = 6;
     public int height = 6;
 
     public HexCell cellPrefab;
     public Text cellLabelPrefab;
 
     HexCell[] cells;
     Canvas gridCanvas;
 
     void CreateCell(int x, int z, int i)
     {
            ......
 
         Text label = Instantiate<Text>(cellLabelPrefab);
         label.rectTransform.SetParent(gridCanvas.transform, false);
         label.rectTransform.anchoredPosition =
             new Vector2(position.x, position.z);
         label.text = x.ToString() + "\n" + z.ToString();
     }

I'm finding that I cannot assign the prefab in the editor, because the type is Text, rather than GameObject. They also use the components directly, rather than using GetComponent<>() which is what I would usually do. What am I missing here, is there a way to create prefabs of types other than GameObjects, or to use prefabs as seen in the code here? It would definitely save some lines of code calling GetComponent and having local variables, but I have never seen this done before and can't get it to work.

Any help appreciated.

,I am trying to follow the CatLikeCoding tutorial here

I am confused by the part where they make labels for the cells - the label is just a game object with the text component, which they then turn in to a prefab.

When they come to use the label prefab in code, though, it is used like this, where the prefab has a type of Text:

 public class HexGrid : MonoBehaviour
 {
     public int width = 6;
     public int height = 6;
 
     public HexCell cellPrefab;
     public Text cellLabelPrefab;
 
     HexCell[] cells;
     Canvas gridCanvas;
 
     void CreateCell(int x, int z, int i)
     {
                 ...
         Text label = Instantiate<Text>(cellLabelPrefab);
         label.rectTransform.SetParent(gridCanvas.transform, false);
         label.rectTransform.anchoredPosition =
             new Vector2(position.x, position.z);
         label.text = x.ToString() + "\n" + z.ToString();
     }

Normally I would have had my public variable be a GameObject which I can assign in the editor, and then get the text and rectTransform components using GetComponent<>(). When I try and follow the tutorial, it unsurprisingly won't let me assign my prefab to the public cellLabelPrefab in the editor because it has the wrong type.

What am I missing? Is there a way to use prefabs as described in this tutorial? If so it would save a lot of uses of GetComponent, but I have never seen it done this way and can't get it to work.

Any help or advice appreciated.

Comment
Add comment · Show 1
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 Klarzahs · Oct 23, 2020 at 11:34 PM 0
Share

Can you upload the inspector view of your cellLabelPrefab? Sounds like it doesn't actually contain the Text component. Did you create it as an UI Element?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zimbwawa · Oct 23, 2020 at 11:19 PM

Did you add a text component to the prefab? Thats what is being referenced in the code. Its a reference to the text component on the gameobject. To assign a gameobject to that reference it must have a text component attached. Hope I helped

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

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

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

Associate UI with gameobject 2 Answers

How do I send inputfield text to another script? 0 Answers

The prefab values keep changing 0 Answers

Getting a animated preview of each prefab in a list? 0 Answers

Prefab Initiated At Beginning Of Game Returns Error When Script On Prefab Attempts To Find A Gameobject In Game 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