• 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 nTu4Ka · Oct 09, 2015 at 07:46 PM · prefab-instance

Can I add a script to prefab to access instantiated objects components?

Hi,

I have a prefab with about 20 children - text and sprites. Can I add a script to it which on instantiating prefab it would generate references to the children's components? So that on runtime I could change values without needing every time to find references.

For example:

 -PanelPrefab
 --Name_TextChild (UI Text)
 --Description_TextChild (UI Text)
 --Icon_ImageChild (UI Image)

Panel prefab assumably has a script.
On certain calling script an object is instantiated from prefab.
On instantiation script attached to prefab will get references to text and sprite components on Text and Image children.
Later I can instantly change these values from calling script via instantiated object by these references.

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

1 Reply

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

Answer by Statement · Oct 09, 2015 at 09:24 PM

Yes, you can do that through a script.

You could either make a script that you apply to the prefab that hold all relevant references in advance, or you could look for them in Awake on that script. If you set the references up on the prefab through the inspector in advance, you don't have to worry about changing names of children breaking your system.

I don't know what your end goal is, but it can be nice to let the script also handle all requests you have for its children, so you don't have to first get them and then apply the algorithm om them.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class YourPanelScriptExample : MonoBehaviour
 {
     // Set the references in the inspector on the actual prefab...
     // If you really want to access the components directly, 
     // just make them plain old public instead...
     [SerializeField] private Text label;
     [SerializeField] private Text desc;
     [SerializeField] private Image icon;
 
     // ... Or call this method in Awake if you prefer to drive it through code.
     private void FindReferences()
     {
         label = FindReference<Text>("Name_TextChild");
         desc = FindReference<Text>("Description_TextChild");
         icon = FindReference<Image>("Icon_ImageChild");
     }
 
     // Just a helper to tidy the code a bit.
     private T FindReference<T>(string child) where T : Component
     {
         return transform.FindChild(name).GetComponent<T>();
     }
 
     // Access your data through properties if you want.
     public string Label { get { return label.text; } set { label.text = value; } }
     public string Description { get { return desc.text; } set { desc.text = value; } }
     public Sprite Icon { get { return icon.sprite; } set { icon.sprite = value; } }
 
     // Or add meaningful methods if you can think of any.
     // public void ChangeSelectedItem(ShopItem item)
     // {
     //    PlayNewSelectionSound();
     //    HighlightPurchaseButton();
     //
     //    Label = item.name;
     //    Description = item.desc;
     //    Icon = item.icon;
     // }
 }
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 Statement · Oct 09, 2015 at 09:31 PM 0
Share

Either you can add the script to the prefab, or you can add it using AddComponent after an Instantiate. If you add the script to the prefab, you have the choice to populate the references design time. If you do AddComponent after Instantiate, then you need to call an equivalent of FindReferences as listed above - preferably in Awake so all references get set before execution return to the caller of AddComponent.

avatar image nTu4Ka · Oct 10, 2015 at 07:37 AM 0
Share

@Statement You're awesome! That is exactly what I was looking for. Also I agree that setting children in inspector is a better idea.

avatar image Statement nTu4Ka · Oct 11, 2015 at 07:55 PM 0
Share

@nTu4$$anonymous$$a I dont know where you comment disappeared but you were asking how to get a reference to the script on the prefab clone. You can call clone.GetComponent() to get it, or, make sure that the prefab reference type already was YourPanelScriptExample to begin with. Etc see second example in docs.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

How do I access variables from a game object from an instantiated prefab 1 Answer

Why does changing an instance affect the prefab? 1 Answer

"Change" Prefab Component Attributes 0 Answers

Can someone explain this error? 1 Answer

[SOLVED] Existed prefab countdown timer bar resetting it's value back to begining after i instantiate a new one. 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