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 deanpackard · Jun 26, 2018 at 01:05 AM · prefabbuttoninstantiate prefabonclicklistener

My onclick action listeners I attach to my buttons as I instantiate them only work once

In my game, I pull a list of games that a player is the admin on from the database. Once I get that list, I instantiate a button for each game that gets pulled from the database, as shown here. alt text

after setting up each of these buttons, I add action listeners to each one, and all I need to send on to their handler is the name that I've attached to the button. What the action handler does is sets the text of the header on the next window to the text of the button that was pressed, this works just fine the first time, but if I go back and click one of the other buttons, the header text stays the same as it was before, and I get a null reference error that looks like this. alt text

here is the offending code:

     //Purpose: create a game button for each game the user manages
 //Parameters: the list of all games that the user is an admin on
 //Returns: nothing
 public void addManagerButtons(string gamesList)
 {
     string[] games = gamesList.Split(';');
     for (int i = 0; i < games.Length - 1; i++)
     {
         GameObject go = (GameObject)Instantiate(gameButtonPrefab);
         go.transform.SetParent(managerScrollField.transform);
         gameName = games[i].Substring(games[i].IndexOf("GameName") + "GameName".Length);
         if (gameName.Contains("|"))
         {
             gameName = gameName.Remove(gameName.IndexOf("|"));
         }
         string admin = games[i].Substring(games[i].IndexOf("Admin") + "Admin".Length);
         if (admin.Contains("|"))
         {
             admin = gameName.Remove(admin.IndexOf("|"));
         }

         if (admin == "true")
         {
             go.transform.name = gameName;
             go.GetComponentInChildren<Text>().text = gameName;
         }
         //I need to make a view that will show edit rules, view users, etc.
         go.GetComponent<Button>().onClick.AddListener(delegate { managingGame(go.GetComponentInChildren<Text>().text); });
     }
 }
 //2. When the button is there, clicking it takes you to a screen that has a few buttons
 //    a.edit some of a games rules
 //  b.look at a list of all players that joined and their target, and pins of both
 //  c.edit a players alive or dead status and their pin

 //Purpose: bring up the managing game view for a specific game, or bring it down if its already up
 //Parameters: the games name
 //Returns: nothing
 public void managingGame(string thisGameName)
 {
     if (!managingGameView.gameObject.active)
     {
         managingGameView.gameObject.SetActive(true);
         StartCoroutine(slideMenu(1f, managingGameView));
     }
     gameText.GetComponentInChildren<Text>().text = thisGameName;
     click.PlayOneShot(click.clip);
 }


2018-06-25-18-46-48-unity-201810f2-personal-64bit.png (5.2 kB)
2018-06-25-18-55-54-unity-201810f2-personal-64bit.png (5.0 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

1 Reply

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

Answer by deanpackard · Jun 26, 2018 at 01:22 AM

The problem had nothing to do with the prefabs. The problem had something to do with how I was accessing the game text. I dont know why, but when I tried accessing the games text a second time using

  gameText.GetComponentInChildren<Text>().text = thisGameName;

Unity wasn't happy with me. I switched my objects reference to the text object itself and access it like this :

 gameText.text = thisGameName;

and it works just fine.

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

122 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

Related Questions

Weird behaviour when trying to dynamically create buttons 0 Answers

Script uses transform.position of Prefab instead of Instance 1 Answer

button.onClick.AddListener(method); NOT Working 1 Answer

A Problem with button listener in Start() 0 Answers

AddListener() Javascript 2 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