- Home /
Question by
Random10000000000000000000000 · Dec 28, 2015 at 07:43 PM ·
c#buttononclick
Unity 5 C# Button.OnClick.AddListener
My code for adding events to my button onclicks dynamically in game does not work. I have gotten other parts of my button to be modified in game, how the onlick code simply will not work and I have tested many solutions. Here is my code (Sorry I don't know how to format) and a few screenshots:
//The method that IS called (I tested), that mostly works,alt text
//Except for the onclick part.
public virtual void activateNode()
{
ConversationController.convCon.button1.onClick.AddListener(delegate { ConversationController.convCon.advanceToNextNode(choice1path); });
//ConversationController.convCon.button1.onClick.AddListener(() => { ConversationController.convCon.advanceToNextNode(choice1path); });
//COMMENT FOR FORUMS: As you can see, I have already tried to use the "=>" solution, as well as the delegate solution but neither have worked
//The choicepath values are also successfully loaded, I checked.
ConversationController.convCon.button2.onClick.AddListener(delegate { ConversationController.convCon.advanceToNextNode(choice2path); });
ConversationController.convCon.button3.onClick.AddListener(delegate { ConversationController.convCon.advanceToNextNode(choice3path); });
ConversationController.convCon.button4.onClick.AddListener(delegate { ConversationController.convCon.advanceToNextNode(choice4path); });
//The text loads perfectly, the buttons text are succsessfully modified so I don't get why the addlistener isnt working for the button
ConversationController.convCon.button1.GetComponentInChildren<Text>().text = choice1;
ConversationController.convCon.button2.GetComponentInChildren<Text>().text = choice2;
ConversationController.convCon.button3.GetComponentInChildren<Text>().text = choice3;
ConversationController.convCon.button4.GetComponentInChildren<Text>().text = choice4;
//This works as well
ConversationController.convCon.speakerText.text = speakerDialogue;
}
Again, I want there to be onclick events, but the onclick events do not appear after I attempt to load them.
screenshot-1.png
(368.1 kB)
screenshot-2.png
(188.2 kB)
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Press button on canvas to transform prefab clone? 0 Answers
OnClick() animation 0 Answers
How do I make a function not appear on UnityEvents / Button OnClick lists? 0 Answers
Random.Range 0 Answers