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 jasonm87 · Jan 07, 2015 at 05:02 PM · gameobjectclassmonobehaviourmethodabstract

Simple abstractions. Reusing code outside of class

Hi,

Lets say i have this code:

 SpriteRenderer sr;
 Sprite sprite1;
 Sprite sprite2;
 
 void Awake()
 {
 sr = GetComponent<SpriteRenderer>();
 }
 
 void OnTriggerEnter2D(Collider2D other)
 {
 sr.sprite = sprite1;
 
 }
 
 void OnTriggerExit2D (Collider2D other)
 {
 sr.sprite = sprite2;
 
 }
 

This is a simple example but imagine i do something like this

 void SwitchSprite (Sprite newSprite)
 {
 sr.sprite = newSprite;
 }


Then in this file I can do

 SwitchSprite(sprite1);

etc.

What if i wanted to use this in my whole project?

should i create a public static void SwitchSprite (SpriteRenderer sr, Sprite newSprite); // ?

is this worth doing in your experience?

Is creating simple abstractions like this a good idea?

thanks

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 Victory Dan Greene · Jan 07, 2015 at 05:14 PM 0
Share

It boils down to: do you save time using

 SwitchSprite( sr, sprite1 ) ;

ins$$anonymous$$d of

 sr.sprite = sprite1 ;

?

Or is there another reason to create such a method?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Baste · Jan 07, 2015 at 05:45 PM

For simple little things like changing the value of a variable, no, probably not.

For bigger things you keep doing a lot, but that doesn't belong together in a separate script or something that doesn't fit withing polymorphism, then yes, you can do this. It's usually referred to as "helper methods".

As a simple example: say you have an enum in your game named "Direction" that you use to specify movement to the north, east, south and west in some grid. Then it would be natural to define a static method MoveInDirection, that moves a vector in the given direction:

 public static Vector3 MoveInDirection(Vector3 position, Direction direction, float length) {
     if(direction == Direction.North) {
         return position + new Vector3(1, 0, 0) * length;
     }
     ....
 }

A great way to do this in C# is through extension methods, which would allow you to do the same, but as a method on the class instead of a static method. If you made the above example as an extension method, you could do this:

 Vector3 myVectorMovedNorth = myVector.MoveInDirection(Direction.North, 5f);

, which is nice. I do this a lot for things like rotating vectors around pivots, rounding Quaternions, and things like that.

As a rule of thumb, if you do the same thing several places in your code, put that thing in it's own method if it's more than one line of code. Be sure to give the methods good names - you shouldn't have to read through a method to understand what it does, the method name should be enough.

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 jasonm87 · Jan 07, 2015 at 06:10 PM 0
Share

perfect answers. 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Do you tend to have scripts to mark prefabs 1 Answer

creating Custom class 1 Answer

Access the GameObject a class is attached to 1 Answer

Alternative to using prefabs w/scripts to save sets of data for use at runtime? 1 Answer

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 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