- Home /
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.
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.
Your answer
Follow this Question
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