- Home /
Animation selector Editor. Losing data at runtime. Awake() returns Debug.Log several times.
Hey, I'm trying to make an editor which allows me to select any of the animations in an object using a popup dropdown. I just slot an object. And it captures the name of the object to find at runtime plus the whole list of animations in the animator/object. The actual popup works, it does show me the animation clips in the object in the inspector, but when I hit play something strange happens. I've setup some debug.logs in Awake() which show me the name to use for searching the object but it appears 17 times. the first time it gives me the name properly, but the next 16 times it returns the name empty (which causes the object's search to be always null).
here is the editor code:
//checking animations -- s means Source/SourceScript
s.TempObject = (GameObject)EditorGUILayout.ObjectField("Object to animate:", s.TempObject, typeof(GameObject), true);
EditorGUILayout.Space();
if (s.TempObject == null)
{
EditorGUILayout.LabelField("There's no object to animate...");
return;
}
s.ObjectName = s.TempObject.name;
//get the animations in the object
AnimationClip[] animations = AnimationUtility.GetAnimationClips(s.TempObject);
//get the clips names into a list
string[] nameArray = new string[animations.Length];
int i = 0;
foreach (AnimationClip clip in animations)
{
nameArray[i] = clip.name;
i++;
}
int index = 0;
index = EditorGUILayout.Popup("Select an animation to play: ", index, nameArray, EditorStyles.popup);
//return the name of the selected animation clip
s.ClipName = nameArray[index];
This is the regular script's code that has to do with this (there's more but it doesn't have anything to do with this- script functions like a hotspot):
//Animation variables
public string ClipName;
public string ObjectName;
public Animator MyAnimator;
public GameObject TempObject;
private GameObject _runtimeObject;
//Then inside a protected virtual void awake()
Debug.Log("name of the object to animate is: "+ObjectName);
//initialize animation variables
_runtimeObject = GameObject.Find(ObjectName);
if (_runtimeObject != null)
{
MyAnimator = _runtimeObject.GetComponent<Animator>();
if (MyAnimator == null)
{
Debug.Log("The animator is null...");
}
}
else
{
Debug.Log("object it's null...");
}
I tried checking if it was because the editor script kept trying to read the object and was overwriting the data. So I put the code inside an if in Unity Editor, but same thing happened. I also tried if (EditorApplication.isPlaying) return; but no difference. And I get the debug.Log with the ObjectName 17 times, even though it's in Awake() (the very first one is always right, the others always return empty...) Any Ideas? I'm kinda running dry of them right now...
Answer by Alverik · Sep 11, 2016 at 04:34 PM
Never mind, it works. The animation isn't playing for a different reason (the animation I was trying to call for my object was set in the Default slot in mecanim, and even if named the same as the animation it didn't seem to work at all... had to make a separate state with the animation name to get it working). Also, Awake() seemed to be running more than once because I wasn't aware there were other objects in the scene sharing the script (I felt so stupid when I found out)...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dynamic editor dll linking? 0 Answers
GetComponent and AddComponent with variables for Collider type 2 Answers
Property Drawer and Inheritance 0 Answers