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 /
This question was closed Dec 11, 2016 at 05:48 PM by Vekosava for the following reason:

Solved

avatar image
0
Question by Vekosava · Dec 10, 2016 at 04:56 PM · uibuttoncharactergetcomponentselection

How to get component of any button that is clicked? (Using same function for every button)

Hey everyone! I'm trying to make character selection screen. I have multiple buttons which represents different characters, also all of them has Character script attached (which is just information like name, prefab..etc...). I want to make system that will get character component from any button that is clicked, but without making multiple functions. That way, I would get prefab of that character and instantiate it in other scene.

If anyone can help, I would appreciate it.

Or if anyone has some better solution,please let me know. 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

  • Sort: 
avatar image
0

Answer by RobAnthem · Dec 10, 2016 at 05:55 PM

I'd just make a string as the reference and then you can easily get the info of your character choice by doing something like this, but maybe I'm overthinking it.

Your main area so that the data exists outside individual buttons

 private string MyString;
 public string myString {
 get { return MyString;}
 set { MyString = value; checkString();}
 }
 
 void checkString()
 {
    if (myString == "Warrior")
    {
    //do warrior stuff
    }
 }

and then in the button

 void Start() { MyButton.onClick.AddListener(() => { MyFunction(); });
 }
 public void MyFunction()
 {
     mainData.myString = choiceList.Find(x => x.choiceName.Equals(gameObject.GetComponents<Text>().Text));
 }



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 Vekosava · Dec 11, 2016 at 05:47 PM 0
Share

I've done it similar way with integers, thanks anyways! :D

avatar image RobAnthem · Dec 11, 2016 at 05:51 PM 0
Share

Yeah numbers would likely be cleaner and take less resources since a string is the equivalent size of an integer * each character. Anyway just make your choices list an outside source and reference based on the buttons parameters. The easier parameter being the Text of the button since it is the only guaranteed difference between each button.

avatar image
0

Answer by Kim-Nobre · Dec 11, 2016 at 11:29 PM

Create a script to store the information of the character selected, it should have the same variables as the "Character" script you attached to the buttons. Let's say the name of the script is "CharacterSelected". Then you create a function on the "Character" script called "SendCharacterSelectedInfo" (or anything else you want), this function should be called in the "OnClick" event of the buttons, and will basically do this:

 void SendCharacterSelectedInfo()
 {
      characterSelected.name = name;
      characterSelected.prefab = prefab;
 }

And then you use the information on the CharacterSelected script as you like it. I hope this can help you.

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

Follow this Question

Answers Answers and Comments

99 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 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 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

Unity5 UI - How to trigger button click event while preventing menu item deselect event? 1 Answer

Char select / GUI button question. 3 Answers

Is Some UI Element Focused or not ? 0 Answers

Button is not selectable when pressing the submit button even though it is highlighted. How do I fix that? 0 Answers

[4.6 b20] How to make a hold button for the new UI 4 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