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 AxisRob · Jan 23, 2014 at 03:40 PM · debugstaticextension-methods

Extension Methods and being Static

I made a class, but I have an issue. I want to write an extension method to parent objects, and to have it be accessible by simply writing gameobject.parent (blah blah). In addition, I wanted to write a debug file that would be useful helping me troubleshoot why my player prefs don't appear to be saving. However, I can't do this because when I tried to use static methods, Unity said extension methods must be in a static class. Fair enough (but since static can't inherit, I'm not sure how it expects mono to work).

The problem is that even after becoming static, I STILL can't access any of the extension methods or members of the class.

Here is the code: using UnityEngine; using System.Collections;

 public static class ExtensionMethods
 {
     
     /// <summary>
     /// Parents the game object, then resets the local rotation, position, and scale
     /// </summary>
     static void ParentResetTransform(this GameObject gameobject, GameObject parentObject, GameObject childObject)
     {
         childObject.transform.parent = parentObject.transform;
         childObject.transform.localRotation = Quaternion.identity;
         childObject.transform.localPosition = Vector3.zero;
         childObject.transform.localScale = Vector3.one;
     }
     
     /// <summary>
     /// Parents the game object without changing anything
     /// </summary>
     static void Parent(this GameObject gameobject, GameObject parentOb, GameObject childOb)
     {
         childOb.transform.parent = parentOb.transform;
     }
 
     /// <summary>
     ///  Checks to see what particle system it is, then sets all particles to a designated layer
     /// </summary>
     static void SetSortingLayer(this GameObject gameobject, string sortingLayer)
     {
         ParticleRenderer[] renderers = gameobject.GetComponentsInChildren<ParticleRenderer>();
         if (renderers.Length != 0)
         {
             for (int i = 0; i < renderers.Length; i++) 
             {
                 renderers[i].sortingLayerName = sortingLayer;
             }
         }
         ParticleSystemRenderer[] systems = gameobject.GetComponentsInChildren<ParticleSystemRenderer>();
         if(systems.Length != 0)
         {
             for (int i = 0; i < systems.Length; i++) 
             {
                 systems[i].sortingLayerName = sortingLayer;
             }
         }
     }
 
 
     /// <summary>
     /// Takes a number of strings and looks for a corresponding playerprefs key then prints the value as a string to the console
     /// </summary>
     static void DebugPlayerPrefs(params string[] playerprefsKey)
     {
         string printToLog = string.Empty;
         string output = string.Empty;
         foreach (string keystring in playerprefsKey)
         {
             if (PlayerPrefs.HasKey(keystring))
             {
                 string debugStandard = string.Format("Playerprefs.{0} is: ", keystring);
                 output = (PlayerPrefs.GetFloat(keystring).ToString());
                 if (output != "0.0")
                 {
                     printToLog += (debugStandard + "a float that is " + output + "\n");
                 }
                 output = (PlayerPrefs.GetInt(keystring).ToString());
                 if (output != "0")
                 {
                     printToLog += (debugStandard + "an int that is " + output + "\n");
                 }
                 output = PlayerPrefs.GetString(keystring);
                 if (output != "")
                 {
                     printToLog += (debugStandard + "a string that is " + output + "\n");
                 }
             }
         }
         Debug.Log(printToLog);
     }
 }

As always, thank you for your time.

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
0
Best Answer

Answer by Jamora · Jan 23, 2014 at 04:02 PM

Your extension methods do not work because your methods have the default access modifier (private). Make your methods public so that they can be accessed.

A little critique: in my opinion, the first two shouldn't be extension methods, because you don't actually use the object you call the method from. Instead, have a static helper class that provides this functionality. The last method isn't an extension method at all (lacks the reference to the caller-object), so it should be placed somewhere else than the ExtensionMethod class.

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

20 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

Related Questions

Debug.Log Override? 3 Answers

Monodevelop 4.0.1 breakpoints won't work. 0 Answers

Debug.log not working when run from mobile 1 Answer

Debug build runs better than release build. 0 Answers

Can Unity track the changes I made to my DLL? 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