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 etliaojing · Jun 29, 2015 at 04:55 AM · c#extension-methods

Texture2D Extension: Texture Still Null After Calling It

I have an extension method for resetting Texture2D:

 public static class MyExtensions
 {
     public static void Reset(this Texture2D tex)
     {
         if(tex == null) tex = new Texture2D(256,1,TextureFormat.RGBA32,false);
         for(int i=0;i<256;i++)
         {
             tex.SetPixel(i,1,Color.clear);
         }
         tex.Apply();
         tex.wrapMode = TextureWrapMode.Clamp;
 
     }
 }

When i try to call it in another class:

 if(ref_tex == null) ref_tex.Reset();
 ref_tex.SetPixel(i,1,new Color(r,g,b,1f));

Unity Log always throws an error on the second line: UnassignedReferenceException: The variable ref_tex has not been assigned.

Do I do something wrong?

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 Inok · Jun 29, 2015 at 05:32 AM 0
Share

i not sure but maybe problem that SetPixel in Reset method work more than 1 frame and you then call SetPixel again until first finish.

1 Reply

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

Answer by steakpinball · Jun 29, 2015 at 02:29 PM

Your best bet is to use a plain old static method with a ref parameter.

 public static class ResetTexture {
   public static void Reset(ref Texture2D tex) {
     if(tex == null) tex = new Texture2D(256,1,TextureFormat.RGBA32,false);
     for(int i=0;i<256;i++) {
       tex.SetPixel(i,1,Color.clear);
     }
     tex.Apply();
     tex.wrapMode = TextureWrapMode.Clamp;
   }
 }
 
 // Somewhere else
 if (ref_tex == null) ResetTexture.Reset(ref ref_tex);
 ref_tex.SetPixel(i,1,new Color(r,g,b,1f));


The reason you can't use an extension method is due to how parameters are passed in C#. Parameters are by default passed by-value. Changing the parameter using = inside the method has no effect on the variable passed to the method.

 void Start() {
   var a = "Hi";
   Debug.Log(a);
   Change(a);
   Debug.Log(a);
 }
 
 void Change(string b) {
   Debug.Log(b);
   b = "Hello";
   Debug.Log(b);
 }

Produces output:
Hi
Hi
Hello
Hi

When you want changes inside a method to propagate back to the caller you use the ref keyword.

 void Start() {
   var a = "Hi";
   Debug.Log(a);
   Change(ref a);
   Debug.Log(a);
 }
 
 void Change(ref string b) {
   Debug.Log(b);
   b = "Hello";
   Debug.Log(b);
 }

Produces output:
Hi
Hi
Hello
Hello

However, the first parameter of an extension method can't be a ref parameter.

 void Change(this ref Texture2D b) {
   // Compile Error: "The parameter modifiers 'this' and 'ref' cannot be used together."
 }

This means you can't use ref to modify the object on which you call an extension method.

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 etliaojing · Jun 30, 2015 at 02:58 AM 0
Share

Thanks brianturner! You saved my day! But im quite confused why it doesn't work for Texture2D. I have a similar extension for AnimationCurve, which works perfectly. Also another for List works.

I feel extension method uses pass-by-ref, cause it modifies the data that passes inside the method. You have any idea on that?

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Is it possible to RPC a C# extension method? 1 Answer

Flip over an object (smooth transition) 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