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 /
avatar image
0
Question by fiso64 · Mar 28, 2020 at 08:01 PM · unity 5methodsextension-methods

Change calling object with extension method

Hello, I've tried creating a very simple function:

 public static void AssignIfNull(this UnityEngine.Object o, UnityEngine.Object obj)
 {
    if (o == null)
    {
        Debug.Log(obj);
        o = obj;
        Debug.Log(o);
    }
 }

However, while Debug.Log(o) outputs the correct object, the real object doesn't change. Is it possible to set the calling object to something else with an extension method?

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
3

Answer by Namey5 · Mar 29, 2020 at 03:38 AM

As much as it probably looks like this should work, both parameters are still being passed by copy, meaning although you are changing the object reference, you're only changing it in the local context of the function. Luckily enough, according to the C# docs;

Beginning with C# 7.2, you can add the ref modifier to the first argument of an extension method.

Meaning in theory you should be able to change the function to something like this;

 public static void AssignIfNull (ref this UnityEngine.Object o, UnityEngine.Object obj)
 {
     if (o == null)
     {
         Debug.Log(obj);
         o = obj;
         Debug.Log(o);
     }
 }

Haven't tried this personally, so there may be some logical thing I'm overlooking here, but this is how I would think of going about it.

Comment
Add comment · Show 5 · 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 fiso64 · Mar 29, 2020 at 08:56 AM 0
Share

Thanks for the help!

Sadly, it appears that combining this with ref doesn't work, in fact it seems that this entire idea of an extension method that modifies the caller is impossible to create that simply. Here I found out why it doesn't work.

avatar image Namey5 fiso64 · Mar 29, 2020 at 09:07 AM 1
Share

That was posted in 2011, long before C# supported the ref modifier with extension methods. If you wish to see the official documentation that says you can do the above, see here;

https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/classes-and-structs/extension-methods

Whether this functionality is supported by the version of C# Unity uses is up for debate, as this would appear to have been implemented in the last year or two. The only way to find out would be to test it.

avatar image Namey5 Namey5 · Mar 29, 2020 at 09:32 AM 1
Share

Ok so I've just done some tests, and it would appear that passing 'this' by reference only works for struct types. However, the principle still applies, and even though you may not be able to do it directly through an extension method, there's nothing stopping you from just writing something like this;

 public static void AssignIfNull (ref UnityEngine.Object a, ref UnityEngine.Object b)
 {
     if (a == null)
         a = b;
 }
avatar image fiso64 · Mar 29, 2020 at 10:20 AM 0
Share

This is what I was getting at. I can do that, but Extension methods with ref are impossible at the moment, and I'm still not entirely convinced that they should be.

avatar image Namey5 fiso64 · Mar 29, 2020 at 12:24 PM 1
Share

Yeah. $$anonymous$$y best guess would be that assigning to an object with the modifier of 'this' (or even assigning to 'this' directly) for class types would end up with unhandled memory, as reassigning the class pointer from within the instance would probably bypass the destructor. Although, in the original case that you are trying to do this, are you trying to point the current instance to the location of the second, or are you simply trying to copy its values?

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

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

Can I increment a static variable from a static method? 1 Answer

Unable to load script, when referencing an extension method 2 Answers

Attached this script to panel object..want to panel rotate in z axis using touch (rotate drag direction on panle)only...but rotating for x,y axis but not z. NOT WORKING Plz HELP 0 Answers

How do i create a Mesh in the middle of a Catmull spline? 0 Answers

Importing non-traditional 512x512,1024 x1024 Height Maps 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