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*.sortingLayerName = sortingLayer;*
-
}*
-
}*
-
ParticleSystemRenderer[] systems = gameobject.GetComponentsInChildren<ParticleSystemRenderer>();*
-
if(systems.Length != 0)*
-
{*
-
for (int i = 0; i < systems.Length; i++)*
-
{*
_ systems*.sortingLayerName = sortingLayer;_
_ }_
_ }_
_ }*_
///
/// Takes a number of strings and looks for a corresponding playerprefs key then prints the value as a string to the console
///
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 + "
");
}
output = (PlayerPrefs.GetInt(keystring).ToString());
if (output != “0”)
{
printToLog += (debugStandard + "an int that is " + output + "
");
}
output = PlayerPrefs.GetString(keystring);
if (output != “”)
{
printToLog += (debugStandard + "a string that is " + output + "
");
}
}
}
Debug.Log(printToLog);
}
}
As always, thank you for your time.