Hello, I have no other way of asking this than: Why doesn’t this work?
using UnityEngine;
public class TestScript : MonoBehaviour {
void Update(){
Debug.Log("This one works, obviously.");
}
public class TestClass_1{
void Awake(){
Debug.Log("This one doesn't work. Why?");
}
}
}
It doesn’t work because your TestClass_1 doesn’t derive from MonoBehaviour, which is where the Awake method is found. You can make your class derive from MonoBehaviour by changing your class declaration to
public class TestClass_1 : MonoBehaviour
{
void Awake()
{
Debug.Log("This one doesn't work. Why?");
}
}
Even after you do this, it won’t run like you expect because you still need to create an instance of the class. When a script derives from MonoBehaiour, Unity doesn’t allow you create a new instance in the normal way. Instead, you need to add the class as a component of your gameobject.
using UnityEngine;
public class TestScript : MonoBehaviour
{
void Awake()
{
gameObject.AddComponent<TestClass_1>();
}
void Update()
{
Debug.Log("This one works, obviously.");
}
public class TestClass_1 : MonoBehaviour
{
void Awake()
{
Debug.Log("This one doesn't work. Why?");
}
}
}
This should print out
This one doesn’t work. Why?
and then:
This one works, obviously.
over and over and over again.