Jump to content

Creating objects that inherit from MonoBehaviour


Recommended Posts

I am going a bit nuts now... it is a long story!

I am trying to create a drop down menu for by mod's GUI. I have tried a few things, and have something that "works" but the problem is that if you click on the dropped down menu, then the button underneath is clicked. Annoying!

Anyhow it turns out that I will be much better off putting all the dropdown GUI stuff into a separate class. This is neater, but also allows be to use GUI.depth, which should solve my problem. As I understand it, if this new class inherits from monoBehaviour, then its OnGUI method should get called.

However it turns out that when I say

GUIDropDown myDropDown = new GUIDropDown();

I get null returned. If I do not inherit from MonoBehaviour, then the new object creation does not return null.

So - what is MonoBeahviour doing, and what do I have to do to create my object?

Link to comment
Share on other sites

Components can't exist on their own. They need to be part of a GameObject. You can just change your code to:

var someGo = new GameObject("GUIDropDown", typeof(GUIDropDown));

// or
var someGo = new GameObject();
someGo.AddComponent<GUIDropDown>();

Edit: You can just add it directly to your mod's existing gameObject instead of creating its own GameObject as well

Edited by xEvilReeperx
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...