Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bokaii · Mar 07, 2015 at 09:10 PM · c#variablestringanother script

Change multiple variables with function argument

I am using the new UI Button. I want a function to put under the OnClick Component. I want a function that I can reuse and just change 1 thing. Thats what I'm trying to do.

How do I change a variable in another script by, with a single function?

like:

  void Change(ThingToChange){
      VariableScript.ThingToChange = "Something";
  }
  
  Not:
  void ChangeTest1(){
      test1 = "blabla";
  }
          
  void ChangeTest2(){ 
      test2 = "blabla2";
  }

 

And so on. That I what is don't want to do! I want to have only 1 void/ function that I can reuse.

Comment
Add comment · Show 1
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 Bokaii · Mar 07, 2015 at 10:27 PM 0
Share

What I want is:

A script with multiple string in $$anonymous$$ultiple buttons I can press to change the strings in the first script. Using a single void/ function for every button.

Thats it. Is there a way to do this?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by d2 · Mar 09, 2015 at 03:37 PM

Maybe you can use Generics:

https://unity3d.com/es/learn/tutorials/modules/intermediate/scripting/generics

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
avatar image
0

Answer by alok-kr-029 · Mar 09, 2015 at 10:42 AM

your question is not to clear but I guess you mean somthing like this

  public  void ChangeTest( string button_fun)
     { 
          switch(button_fun)
     {
        case "test1":
             print("dosmthing");
     break;
        case "test2":
             print("dosmthing2");
     break;
     
     }
     
       }

by passing the parameter to the button and making the case you can reuse the function One single function for all the button

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 fafase · Mar 09, 2015 at 10:48 AM 0
Share

This is the base code of what is consider spaghetti code. You will end up with one function doing all kind of things unrelated to each other. Debugging becomes a nightmare and you can forget about efficient scalability.

Better to have a lot of small functions that do only what they are meant to. It may seem like annoying at first because you need to drag them all but when a problem occurs, you need exactly where and why.

avatar image Bokaii · Mar 09, 2015 at 03:15 PM 0
Share

I'll just go with the multiple functions. But thanks :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find variable in another script using a string 1 Answer

string + int = variable 2 Answers

UI button change variables in another script 1 Answer

Multiple Cars not working 1 Answer

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 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