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 Magnet_man16 · Jan 12, 2016 at 04:51 PM · c#variablesfunctionsstruct

How can you add a function to a variable. Such as string.ToUpper() ?

I've been programming in C# for about 5 years. However I've always wondered how do you do like below:

 public struct Vector3
 {
             public float X,  Y, Z;
 }
 
 public Vector3 Positionlol;
 
 void Start()
 {
     Positionlol.Reset();
 }

What do you do to be able to call a function on a variable. I dont want to have to say Reset(Positionlol) and I want to learn how to do it this way, if possible. I assume for example string.ToUpper(); calls a function which operates on the string directly?

I also want to learn this encase I want to create my own type for another crappy engine my university use, which doesn't have things such as Vector3, instead you alter variables as separate floats.

Thanks, Alex

Comment
Add comment · Show 6
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 gjf · Jan 11, 2016 at 01:30 AM 0
Share

5 years with c# and never came across extension methods? google is your friend... ;)

avatar image Magnet_man16 gjf · Jan 11, 2016 at 09:48 AM 0
Share

I know about extention methods. However I don't know how to have it so I could do Vector3 v = new Vector3(0, 10, 0); ?

avatar image gjf Magnet_man16 · Jan 11, 2016 at 01:32 PM 0
Share

extension methods answer your first question which was:

"What do you do to be able to call a function on a variable. I dont want to have to say Reset(Positionlol) and I want to learn how to do it this way, if possible. I assume for example string.ToUpper(); calls a function which operates on the string directly?"

for the second part (in future, try to ask each question separately - you're more likely to get timely answers)

 Vector3 v = new Vector3(0,10,0);

is perfectly valid in unity - did you try it? any errors?

Show more comments
avatar image cjdev · Jan 13, 2016 at 02:32 PM 0
Share

string is actually System.String, see this stackoverflow answer. You might check out the System.String $$anonymous$$SDN docs to see how they've implemented it. $$anonymous$$eep in $$anonymous$$d though that a string is a reference not a value like a struct is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vintar · Jan 12, 2016 at 08:31 PM

You can do something like this I guess :

 public static void ResetVector(this Vector3 vector)
     {
       vector = new Vector3(1, 1, 1);
     }

then

 Vector3 position = new Vector3(1, 2,3 );
 position.ResetVector();

All a bit pointless for vectors, but you get the idea.

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

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

56 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

Related Questions

How can I change a function from another script? 1 Answer

IEnumerator Not Completing Entire Function 0 Answers

Getting which function affects a GameObject 0 Answers

Function not running everytime 0 Answers

Game highscore file writing 0 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