Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 raj727 · Aug 17, 2016 at 12:44 PM · buttoncustombutton trigger events

Please help !! Custom Button Event Not responding !!

Here is my Code. The Function doSomething() wont execute on button click. Can you tell me what i am doing wrong. Thanks

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Arrow : MonoBehaviour {

 public GameObject myarrow;  

 private Button myButton; 

 void Start () {
     
     myarrow = GameObject.Find("Arrow_Sprite");

     myarrow.AddComponent<Button> ();
 //    myarrow.GetComponent<Button> ().enabled = true;

 //    myButton.GetComponent<GameObject> ().gameObject = myarrow.gameObject;

 //    myButton = myarrow.GetComponent<UnityEngine.UI.Button> ();  
     myarrow.GetComponent<Button>().onClick.AddListener (delegate {
         doSomething(); 
     });

     myarrow.GetComponent<Button>().enabled = true;

 }

 public void doSomething()
 {
     myarrow.transform.position = new Vector2 (5, 2); 

 }
 // Update is called once per frame
 void Update () {
 
 }

}

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 aditya · Aug 17, 2016 at 01:37 PM

Try this ...

  public GameObject myarrow;  
  void Start () {
      myarrow = GameObject.Find("Arrow_Sprite");
      myarrow.AddComponent<RectTransform>();
      myarrow.AddComponent<Button> ();
      myarrow.GetComponent<Button>().onClick.RemoveAllListeners();
      myarrow.GetComponent<Button>().onClick.AddListener (doSomething);
      myarrow.GetComponent<Button>().enabled = true;
  }
  public void doSomething()
  {
      Vector3 newPos = new Vector3(5f, 2f, 0f);
      newPos.z = myarrow.transform.position.z;
      myarrow.transform.position = newPos; 
  }
Comment
Add comment · Show 3 · 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 raj727 · Aug 19, 2016 at 05:48 PM 0
Share

Thank you. I will try this. Thanks for responding !!

avatar image raj727 · Aug 19, 2016 at 06:00 PM 0
Share

Its doing nothing. Just so you know.

  1. Its a 2d game

  2. I have not made an instance of the arrow on the scene.

How do i link this arrow in the inspector. It is a prefab. I am trying to instantiate it via code alone. It has to be a sprite instantiated as a button via code.

avatar image aditya raj727 · Aug 22, 2016 at 07:42 AM 0
Share

Okay ... if you are instantiating UI elements at runtime then unity manual is a must see Creating UI Element at Runtime ... and paste my above code in the same script which is instantiating your button ... the change the first line of my code to public Button myarrow; ... then simply assign this newly instantiated button to myarrow

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

Buttons not working properly. 0 Answers

UI button in Panel not clickable 2 Answers

Custom Android Textfield and Button 0 Answers

Use Singleton Prefab in Button's onClick editor field 0 Answers

How do I create a Gmod item spawn menu in unity? 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