Custom Class Questions

I’ve just started creating custom classes and I’ve run into a couple of probems…

  1. Can I use gameObject in a custom class? I tried a simple Destroy(gameObject) and it gave me an error.

  2. can I use OnTriggerEnter or other non-custom methods inside a custom class? OnTriggerEnter seemed to fail without an error.

thanks

Kevin Abbott

Does your class inherit from or extend another class? And if so, which class?

  1. You can use it but to use Destroy you need to be on a MonoBehaviour extend as it is a monobehaviour function (as most things you would use so going this path makes sense)

  2. Same as on 1, its a functiona available on monobehaviour only
    Here in addition it requires that the script is attached to a game object with a rigidbody or collider set as trigger

So in short: if you extend MonoBehaviour its fine (or if its one that extends MonoBehaviour)
If you extend ScriptableObject, UnityEngine.Object or anything totally different, then it will not work

Yes, right now it extends System.Object. What else changes if I extend MonoBehvior instead?

You get all the behavior of MonoBehavior in addition to your own. Are you familiar with Object Oriented concepts? If not, best look over some tutorials.

http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html