I derived a class "Route" from GameObject and it contains some member variables. I then attach a script to it and access the game object by casting it to Route
Route route = gameObject as Route;
During game mode this turns up as null... Since its derived from GameObject it should automatically get serialized right?
Reading what you intend to do with this. I'm looking forward to a working solution, too.
Answer by Mads Bgeskov
Feb 08, 2010 at 06:54 PM
What I am guessing is that you have created a script in your project that inherits from GameObject. However the GameObject that you have dragged into the scene is not of that type, but still GameObject and the as operator will therefore not be able to cast this type to Route.
If caching is not possible then what about creating an empty gameobject with all the globally required scripts ?
Or, just create a static class ?
If you need a script to be serializable then you must either inherit from MonoBehaviour or ScriptableObject. And if you require the data to persistent then you must look into creating custom assets.
Answer by Jaap Kreijkamp
Jan 11, 2010 at 01:41 PM
Why would you want to derive from GameObject? A gameobject is a gameobject and it's capabilities is determined by the components attached. If you need the gameobject to present itself in special ways in the scene or the inspector you can do this with custom Editors that link to your class using [CustomEditor(typeof(YourClass))] metadata (and YourClass is derived from MonoBehaviour not GameObject!) and override methods OnInspectorGUI and/or OnSceneGUI to display their special things.
I have generic data that all my game objects need. For example the event manager. I have a simple event system that i use instead of SendMessage (due to performance issues) now if i need to send an event to a game object i have to get the EventManager Component through GetComponent this again is bad for performance. I have looked into the option of caching but in my case its not an option.
so to avoid these things i would like the put the event manager in the gameobject using the above method.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
No one has followed this question yet.
The name 'Joystick' does not denote a valid type ('not found')
Updating object on inspector value changes in editor
breaking out of OnGUI() ticks?
Level of details in a script-made mesh in the sceneview
Debug.Log doesn't work properly in a ContextMenu function