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 Maggi1417 · Jul 21, 2017 at 09:09 PM · textprefabslistsaccess

Accessing a certain prefab and it's children

I have a list of items that is used to instantiate a list of button prefabs. Each of this buttons has a Text child that displays the name of the item attached to it/the item that spawned it.

I tried to attach a Script to these buttons that displays the values of the item the button referes to in another Panel. I tried to do it with a loop that searches through the item list and stops when the textdisplay on the button is equal to the item name. This however always returned the values of the first item in the list. Via Debugging I figured out that the script doesn't compares the display text on the button that was clicked but all display texts on all prefab clone buttons. So I obviously need a way to specify that I only want to compare the text on the button that was clicked and not all of them. I spent the last three hours googling for a solution and tried out different things, but nothing worked so far.

The most recent version looks like this:

 private void OnMouseDown()
     {
         SetWriterData(gameObject.GetComponentInChildren<Text>());
        
     }
 
  public void SetWriterData(Text currentname)
     {
        
 
 
         for (int i = writerlist.Count - 1; i >= 0; i--)
         {
             
 
             if (currentname.text == writerlist[i].name)
             {
                 
 
 
                 Name.text = writerlist[i].name;
                 Intel.text = writerlist[i].intelskill.ToString();
                 Complex.text = writerlist[i].complexskill.ToString();
                 Pace.text = writerlist[i].paceskill.ToString();
                 Dialogue.text = writerlist[i].paceskill.ToString();
                 Humor.text = writerlist[i].humorskill.ToString();
                 
             }
         }
 
     }

How can I solve this problem? Is what I'm trying to do possible? Is there a better way to do it?

Comment
Add comment · Show 2
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 Reynarz · Jul 21, 2017 at 10:13 PM 0
Share

Can you show us how you populate the writerlist?

avatar image Maggi1417 Reynarz · Jul 21, 2017 at 11:13 PM 0
Share
        public static List<$$anonymous$$ovieClass.Writer> writerlist = new List<$$anonymous$$ovieClass.Writer>();
     public GameObject WriterDisplay;
     public Transform WriterDisplayPanel;
     public Text WriterNameText;
 
     public Text Name;
     public Text Intel;
     public Text Complex;
     public Text Pace;
     public Text Dialogue;
     public Text Humor;
 
     
 
 
     public void GetWriterList()
     {
         StreamReader reader = new StreamReader("C:\\Users\\magda\\Desktop\\FilmStudioTycoon\\People\\Writer.txt");
 
         
 
         string s = reader.ReadLine();
 
         while (s != null)
         {
 
             char[] delimiter = { ',' };
             string[] fields = s.Split(delimiter);
 
 
             writerlist.Add(new $$anonymous$$ovieClass.Writer(fields[0], int.Parse(fields[1]), int.Parse(fields[2]), int.Parse(fields[3]), int.Parse(fields[4]), int.Parse(fields[5]), int.Parse(fields[6])));
             s = reader.ReadLine();
             
 
             
 
 
 
         }
     }
 
     void SetWriterList()
     {
         
         foreach ($$anonymous$$ovieClass.Writer writer in writerlist)
         { 
         
 
         
         GameObject NewWriterDisplay = Instantiate(WriterDisplay);
 
         NewWriterDisplay.transform.SetParent(WriterDisplayPanel);
 
             WriterNameText.text = writer.name;
 
 
         }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by eskivor · Jul 21, 2017 at 11:53 PM

If I understand you well, you want to only access to the button clone that you have spawned and not directly to the button prefab itself (whose the text is the same for all), that's right ?

To access only to the clone that spawned, you have to create a reference of it first when you spawn the prefab on the scene.

To do that, when you have declared your prefab GameObject variable like this :

 //Here's the variable you have linked to the prefab in the inspector
 public Gameobject buttonPrefab;

when you instantiate the prefab do that :

 GameObject buttonClone = Instantiate (buttonPrefab) as GameObject;

instead of just

 Instantiate (buttonPrefab);

then re-use your reference to access to the component you want, for example :

 Text buttonCloneText = buttonClone.GetComponent<Text>();
 
 if (buttonCloneText != null)
 {
 //Do something here
 buttonCloneText.text = "I'm a button clone !";
 }
Comment
Add comment · Show 2 · 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 Maggi1417 · Jul 22, 2017 at 06:21 AM 0
Share
 Text buttonCloneText = buttonClone.GetComponent<Text>();

This line is not working for me. On it's own it won't compile because "the name "buttonClone" doesn't exist". When I put GameObject buttonClone in the beginning of the script, it complies but Unity throws an error NullReferenceException: Object reference not set to an instance of an object.

I guess the problem is, that the Clone is created in a diffrent function and only exists there?

avatar image eskivor · Jul 22, 2017 at 03:05 PM 0
Share

For the variable GameObject buttonClone, to re-use it in another method you can :

Either declare it as a global variable like this :

 publiic class $$anonymous$$yClass : $$anonymous$$onoBehaviour
 {
     GameObject buttonClone;
 
     void $$anonymous$$ethod1 ()
     {
         buttonClone = null;
         Debug.Log ("buttonClone : " + buttonClone);
     }
 }

Either declare it as a local variable, in a method and send it as a parameter when you use another method, like this :

 publiic class $$anonymous$$yClass : $$anonymous$$onoBehaviour
 {
     void $$anonymous$$ethod1 ()
     {
         GameObject buttonClone = null;
         $$anonymous$$ethod2 (buttonClone);
     }
 
     void $$anonymous$$ethod2 (GameObject _buttonClone)
     {
          Debug.Log ("buttonClone : " + _buttonClone);
     }
 }

And if you have a null reference exception, it's because you are trying to access to an object which is null. You can use a if (nameofYourObjectVariable != null) {} to avoid the error, ins$$anonymous$$d it will just do nothing, to execute the method well, check before that your object is not null (here I suppose that you didn't assign your buttonPrefab via the inspector).

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

74 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

Related Questions

Display text above prefabs in Unity 4.6 0 Answers

Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers

Dynamically assign each ui.Text.text in list to the name of objects in another list 2 Answers

Is constantly referencing or defining once then changing better 1 Answer

Dictionary Keys to Stack Enemy Types 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