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 rainbow_design · Dec 21, 2015 at 07:34 AM · unity 5buttonscript.image

initiate Imagebuttons in loop

Hello, i have a prefab which is an image and creates a map in a loop and i want to attach a click receiver. Right now it looks like this (this is not the complete code just a part to give you an idea)

 for (int y = 0; y < resolutiony; y = y + 1)
 {
 for (int x = 0; x < resolutionx; x = x + 1)
 {
 if (modus == 2) { displaymap(i, x, y); }
 private void displaymap(int i, int x, int y) {
 Sprite mysprite = UnityEngine.Resources.Load("Tiles/" + str(tiname));
 GameObject o = Object.Instantiate(hexbasetile);
 o.GetComponent().sprite = mysprite;
 o.transform.localPosition = new Vector2(((x * xbase)+xdbase), (y * ybase));

Now i want to attach something like this: On Click call beenclicked(o);

 private void beenclicked(GameObject o){


The tutorials on this are extremely thin. How can i do this from code side?

Please provide me some example code or a link! : with instructions only i will not be able to do much. The Essential question is how can i execute a function from a button

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
1
Best Answer

Answer by corn · Dec 21, 2015 at 04:36 PM

Well, the easiest way would be to have a UI Button attached to your prefab. Then you can retrieve it in your script with a simple GetComponent() call, and listen to its OnClick UnityEvent.

 // Get your prefab's button
 Button b = o.GetComponent<Button>();
 // Assign your sprite to the button as its graphic component
 b.image = mysprite;
 
 // Add a listener to your button's OnClick UnityEvent
 b.OnClick.AddListener(myCallback);
 
 void myCallback() 
 {
   //Called whenever b is clicked
 }

Alternatively, you could also attach a script to your prefab that raises an event whenever it detects a click, with

 UnityEvent onClick = new UnityEvent();
 
 if ( Input.GetMouseButtonDown(0) )
 {
   onClick.Invoke();
 }

And add a listener to yourscript.onClick in the same manner.

If you're not familiar with UnityEvents, check the manual.

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 rainbow_design · Dec 24, 2015 at 08:16 AM 0
Share

Oh thankyou. However i think i missed one little detail. I would like to pass o to myCallback on click

avatar image corn rainbow_design · Dec 24, 2015 at 11:09 PM 0
Share

You can call an UnityEvent with arguments, so one way to do it would be to invoke the script's GameObject.

 class GameObjectEvent : UnityEvent<GameObject> { }
 GameObjectEvent onClick = new GameObjectEvent();
 onClick.Invoke(this.gameObject);

And modify your callback accordingly :

 void myCallback(GameObject go) 
 {
   // do stuff
 }

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

39 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

Related Questions

How to Change transparency of button image in Unity 5.0? 3 Answers

Is their any way to save a particle system when it's not instantiated yet? 2 Answers

Button's sprite swap works fine but it doesn't change the related image's source image! why? 1 Answer

Making an image appear from a different script 0 Answers

How I can change the sprite with resources.Load? 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