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 /
  • Help Room /
avatar image
0
Question by Frunobulax · Mar 28, 2016 at 02:53 PM · instantiatebutton

Instantiate buttons

Hi! I have trouble with instantiated buttons. How can instances access the main script they were instantiated from? I want to determine which button is clicked.

Main.cs

 public GameObject myButton; //drag & drop in Inspector
 //myButton is a prefab with attached script
 
 for(int i=0;i=10;i++){
     GameObject b= Instantiate(myButton, new Vector3(5*i,0,0), Quaternion.identity) as GameObject;  
     b.GetComponent<ButtonScript>().ID = i; //tell new button his ID
 } 
 

ButtonScript.cs

 public int ID;
 
 void Clicked(){
     Debug.Log("Hi! I am button nr "+ID); // few drag+drops in Inspector, works fine.    
 }

The problem is that I cannot access a method or field in Main.cs. This makes sense because the buttons are created in runtime. Therefore any reference with objects in the scene cannot be guaranteed. So I understand the problem, however I don't see an elegant solution.

Can I give a reference, just as I would do in more 'classic C#' calling constructor-method: button = new Button(this); (or something like it)

Any thoughts? Thanks!

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

2 Replies

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

Answer by TanselAltinel · Mar 29, 2016 at 01:10 PM

Hi @Frunobulax, There are many ways to accomplish what you want, but easiest way might be to get Main reference in the button script like this:

ButtonScript.cs

 public int ID;
 private Main mainReference;
         
 void Start() {
     mainReference = FindObjectofType<Main>();
 }
 
 void Clicked(){
     Debug.Log("Hi! I am button nr "+ID); // few drag+drops in Inspector, works fine.
     mainReference.anyFunction(ID);    
 }

So, there you have it, you can access Main functions.

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
avatar image
0

Answer by Frunobulax · Mar 29, 2016 at 05:13 PM

Thanks @TanselAntinel! This will work indeed and is very practical. A bit of a drawback is the lack of encapsulation. If I would rename Main, it wouldnt work anymore. I can imagine there are different solutions that are considered good practice. Can you give another solution?

Behind my question about instances lies a deeper quest for good practice of architecture :-)

Thanks for your help, it is much appreciated!

Comment
Add comment · Show 1 · 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 TanselAltinel · Mar 29, 2016 at 05:18 PM 0
Share

Are you not using an IDE? Like Visual Studio?

Refactoring is always a problem if you don't have a tool like that, and if you are working on a professional project, then it will going to be a very big problem for you.

I could provide tens of ways to do this, but it was just an idea. Good practice always depends on what you want to do.

Send$$anonymous$$essage is another option for you, but I would never use that myself.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to create a button (or anything) but out of scene? 0 Answers

scroll rect UI buttons all do the same thing 0 Answers

Instantiating Button/Text Prefab Does Not Work 0 Answers

calling 3D model with button using INSTANTIATE I did successfully with keyboard now I am struggling to call from UI Button 0 Answers

Instantiate a canvas with a button when trigger 0 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