Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Miguet · Jul 23, 2015 at 10:41 AM · scripting problemgameobjectprefabruntime-generation

Unable to attach script to GameObject prefab at runtime

I've got a script that manages a scene browser within my application where I'm hoping to generate a new button instance for each scene found. I've put together a button that I've made into a prefab and everything works up until I try to attach click behavior to the button. So far I am unable to capture or attach anything to the "onClick()" piece of the button component no matter what I try.

Code is as follows:

 MySceneBrowser.cs
    foreach(string sceneName in sceneList){
      GameObject sceneButton = Instantiate(SceneButton) as GameObject;
      // Attach the scene name to the nested text UI object of the button
      sceneButton.GetComponentInChildren<Text>().text = sceneName; 
   /* Where I hope to attach script and method with parameter, method is within 
    * this class and called LoadSelectedScene(string sceneName)
    */
      sceneButton.transform.SetParent(SceneListPanel, false);
      }

Where in the inspector SceneButton is assigned to the prefab I've created, SceneListPanel is set to a content panel, and the scene list is generated in another method.

Any attempt I make to get the button component and allow me to set the onclick() fails, it just doesn't seem to exist through the script editor.

Setting any of these items through the inspector works, but I can't assign my controller class to the prefab before RunTime. I tried attaching a script directly to the prefab, but in this case only MonoBehavior functions were present through the inspector and I was unable to find any other method.

Once I attach the script I also need to be able to add the sceneName as the parameter.

[Edit] DMGregory pointed out I didn't elaborate on what I've tried so far, at this time I've tried the following (likely very wrong, still new to Unity) attempts to attach the desired script method:

  • sceneButton.AddComponent< SceneBrowserController >().LoadSelectedScene(sceneName);

  • Button b = sceneButton.GetComponent< Button >(); b.OnClick()...<--- Script can't find onClick, doesn't exist, doesn't show it as an option in code completion, etc.

  • sceneButton.AddComponent< SceneBrowserController >.LoadSelectedScene(sceneName); <-- obviously doesn't add it to the onClick listener, just attaches the script

  • SceneButton.AddComponent< UIEventListener >()....

  • UIEventListener.Get(sceneButton).onClick().....

This is in general the attempts I've made so far, with some variation. I don't have all my old attempts saved unfortunately. I'm sure I'm making a newbie assumption/mistake somewhere.

[Update] While playing with the prefab through my script I went ahead and tried this out:

 if(sceneButton.GetComponent< Button >()){
 Debug.Log("This object is here");
 }

and it returns false on the if, if I change the if to (!sceneButton.GetComponent()) it does show the log message. To my understanding this indicates the button game object is not found, yet it is a component on the top level of the prefab.

[Update 2] Can anyone explain why this doesn't work to attach the onclick listener?

 sceneButton.GetComponentInChildren<Button>().OnClick.AddListener(() => LoadSelectedScene(scene));

Gives the following compiler error:

 Assets/.../.../.../SceneBrowser.cs(347,68): error CS1061: Type `Button' does not contain a definition for `OnClick' and no extension method `OnClick' of type `Button' could be found (are you missing a using directive or an assembly reference?)

[Update 3] As close to showing my full code as possible:

 using UnityEngine;
 using UnityEngine.UI;
 using System;
 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 
 public class MyFileBrowser : MonoBehaviour
 {
  Public GameObject SceneButton; // This is assigned to the prefab in inspector, they render properly.
  [...]
 
  public void LoadSelectedScene(string sceneName){
   // This is the method I'm hoping to reference in the onClick listener, while passing it the scene name
  }
 
 [...]
 
 private void DisplaySceneList(string[] scenesOutput){

    foreach(string sceneName in sceneList){
      GameObject sceneButton = Instantiate(SceneButton) as GameObject;
      // Attach the scene name to the nested text UI object of the button
      sceneButton.GetComponentInChildren<Text>().text = sceneName; 
   /* Where I hope to attach script and method with parameter, method is within 
    * this class and called LoadSelectedScene(string sceneName)
    */
      // Current attempt:
      sceneButton.GetComponent<Button>().onClick.AddListener(() => LoadSelectedScene(sceneName));
     // The onClick.AddListener[...] is all red in MonoDevelop, hover message says "'Button' does not contain a definition for 'onClick'
      

      sceneButton.transform.SetParent(SceneListPanel, false);
      }

 }

This is with a prefab with the following structure:

  • SceneBrowser_SceneSelectButton (Image Component, Button Component, Layout Element Component)

  • Scene Image (Image component w/ sprite)

  • Scene Label (Text component)

I can access the Scene label no trouble with "GetComponentInChildren< Text >.text [...]" but cannot get "GetComponent< Button > on the sceneButton object created from this prefab. Oddly, "GetComponent< Image > does capture the image component off the top level object just fine.

Comment
Add comment · Show 4
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 Miguet · Jul 23, 2015 at 01:35 PM 0
Share

Direct question here, why doesn't this call work , everything onClick and forward is in red. I've added a new button object as a child on the prefab so it should find that the way it did when I pulled up text (Shown in the original question)

 sceneButton.GetComponentInChildren<Button>().onClick.AddListener(() => LoadSelectedScene(sceneName));
avatar image Runalotski · Jul 23, 2015 at 04:01 PM 0
Share

https://www.youtube.com/watch?v=GpPWb$$anonymous$$_6DVY&list=PLW2i42bgplOkh$$anonymous$$1iuOecfGyQWd_ym$$anonymous$$ap5∈dex=1

This is a good tutorial for making UIs and buttons dynamic each video is short and comes together i think watching the first 4 will cover your issue

avatar image Miguet · Jul 23, 2015 at 04:58 PM 0
Share

Thanks I'll look over the videos right away

avatar image maccabbe · Jul 24, 2015 at 02:14 PM 0
Share

I just tried the following in a scene with a camera, canvas, and eventsystem in addition to a button prefab in my /Resources folder and it worked for me.

 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 public class $$anonymous$$yFileBrowser : $$anonymous$$onoBehaviour
 {
     public GameObject SceneButton;
 
     void Start(){
         DisplaySceneList(new string[1]{"scene1"});
     }
     public void LoadSelectedScene(string sceneName){
         Debug.Log(sceneName);
     }
     
     private void DisplaySceneList(string[] scenesOutput){        
         foreach(string sceneName in scenesOutput){
             GameObject sceneButton = Instantiate(SceneButton) as GameObject;
             sceneButton.GetComponentInChildren<UnityEngine.UI.Text>().text=sceneName; 
             sceneButton.GetComponent<Button>().onClick.AddListener(() => LoadSelectedScene(sceneName));
 
             sceneButton.transform.SetParent(transform, false);
         }        
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How could I create a prefab from an exsisting gameobject at runtime? 2 Answers

Particle Shaders not rendering if Instantiated 0 Answers

Cloned/Instantiated GameObject's Losing Public Variables When Created? 1 Answer

scripting and prefab saving error 0 Answers

Bullet Instantiation 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