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 wolgy88 · Jan 29, 2018 at 11:34 PM · buttonsclassesconstructors

Buttons and choosing class constructor

Hello, I have a question about making choices based on button presses. My title might not be accurate and finding the exact words to ask is probably part of my problem finding the answer. So I do apologize for that in advance.

I have the following code which forms a class and has an initiator(?) inside of it I then have two class constructors and finally public methods that print the values of these constructors based on the button that the respective method is attached to.

 using UnityEngine;
 using System.Collections;
 
 public class testScript : MonoBehaviour {
 
     public class testing{
     
         public int generic_variable;
     
         public testing(int gen_var){
         
             generic_variable = gen_var;
         
         }
     
     }
     
     public testing test_1 = new testing(1);
     public testing test_2 = new testing(2);
     
     public void ButtonCheck1(){
         Debug.Log(test_1.generic_variable);
     }
     
     public void ButtonCheck2(){
         Debug.Log(test_2.generic_variable);
     }
 
     
 }

I've tested this out in game and it works perfectly. My question is how can I make this more dynamic and reduce this down to 1 public method that will print the right variable based on which button is pressed.

I know the simple answer is for the buttons to pass some sort of a variable and then to have an if else statement that is conditional based on the variable passed. However, as easy of a solution this is for 2 buttons, I want to be able to eventually expand this to several buttons and I don't want to have this rigid, predetermined length if else statement determining the conditions. I'm thinking it would be something like, the button would pass along the variable, let's say test_x and then the function prints out test_x.generic_variable but I know that coding is not that simple.

Thank you in advance for your help and I would be happy to answer any questions that would help clarify what I am looking to do.

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 Alanisaac · Jan 29, 2018 at 11:49 PM

What you're probably looking for is a list of your classes. You can use the index of the class in your list to find the right data for the particular button. In the script below, I named the test class "ButtonData" to hopefully make the connection more clear:

 public class TestScript : MonoBehaviour 
 {
     public List<ButtonData> ButtonDatas = new List<ButtonData>();
     
     // Use this for initialization
     void Start()
     {
         ButtonDatas.Add(new ButtonData(1));
         ButtonDatas.Add(new ButtonData(3));
         ButtonDatas.Add(new ButtonData(7));
         ButtonDatas.Add(new ButtonData(0));
         ButtonDatas.Add(new ButtonData(5));
     }

     void ButtonCheck(int index) 
     {
         // because you have 5 items in your list, the index should be a number 0-4
         var buttonData = ButtonDatas[index];
 
         // here you can retrieve any variable from the button data
         var myButtonNumber = buttonData.MyNumber;
         
         // as an example, for an index of 2, myButtonNumber would be 7 
         Debug.Log(myButtonNumber);
     }
 }


 public class ButtonData
 {
     public int MyNumber;
 
     public ButtonData(int myNumber)
     {
         MyNumber = myNumber;
     }
 }

Edit: added a few more comments.

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 wolgy88 · Jan 30, 2018 at 12:17 AM 0
Share

I think I'm following what you're saying, but If I remember correctly C# only allows 1 var type for a list. Although this would technically work for this basic proof of concept example, it would break down if you have different variables types for your class correct?

EDIT - It finally clicked. It's not a list of values to be passed to the constructors, its a list OF constructors. Everything else fell into place after that, thank you.

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

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

Action queue/list for NPCs in point and click adventure. 0 Answers

How to structure achievements? 1 Answer

call a class in another class 2 Answers

Displaying high score on another scene and saving high score 1 Answer

Funtion called without parenthesis 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