x


Saving custom GameObject in editor mode

Hello,

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

  void Start()
  {
         Route route = gameObject as Route;
  }

During game mode this turns up as null... Since its derived from GameObject it should automatically get serialized right?

thanks Pranay

more ▼

asked Jan 11, 2010 at 12:58 PM

pkamat gravatar image

pkamat
483 28 23 30

Reading what you intend to do with this. I'm looking forward to a working solution, too.

Apr 08, 2011 at 10:16 PM by0log1c
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Feb 08, 2010 at 06:54 PM

Mads Bgeskov gravatar image

Mads Bgeskov
91 6 4 10

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 11, 2010 at 01:41 PM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
7k 43 45 93

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.

Jan 11, 2010 at 02:40 PM pkamat
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8584
x465

asked: Jan 11, 2010 at 12:58 PM

Seen: 2263 times

Last Updated: Jan 11, 2010 at 12:58 PM