Issue with instances of objects and game objects in C#

I’m trying to create an instance of an object from a class so that I can keep that object alive throughout the entire game. The object is full of methods for social connectivity so it needs to remain active the entire game in case a player wants to interact with their social media accounts. After running into the issue of not being able to use the new keyword because the class inherits MonoBehaviour I tried instantiating a game object with the social class script attached to it but I can’t access the methods as I would in a C# object. So far it looks as though the only two options are either not have it inherit MonoBehaviour or make all my methods static so that I may access them directly from the class instead of an object.

Well, do you need it to be a MonoBehaviour? This just corresponds to a script attached to a GameObject so it can’t work “on its own” like a regular class instance. You can make it not inherit MonoBehaviour and just instantiate it as a normal object:

void Start () {
    ObjectClass myObject = new ObjectClass();
}

I’m not sure if I need it or not. I’m using things like Instantiate() from MonoBehaviour. Now when I remove the inheritance from Mono and add it explicitly to the method I get a warning(below). Not sure if its an issue or not. I also won’t be able to use the Awake() method if I don’t inherit which really isn’t a big deal because I can just call the method on some event anyway.The class defined in the script file named 'Social' is not derived from MonoBehaviour or ScriptableObject!

As an example.

We use the Service Locator Pattern in our games. The actual service locator is a MonoBehaviour, but many of the individual services are not.

// This is not a very robust service locator!
public class ServiceLocator : MonoBehaviour {

  public NonMonoBehaviourService service { get; private set; }

  void Awake() {
    service = new NonMonoBehaviourService();
  }

}

So we do have a MonoBehaviour to start and access the services even though the services are not MonoBehaviours.

Why does Instantiate not work? …

a Monobehavior is ment to be a Component, that said you need to eather Instantiate it or you add the component to a already existent GameObject.

// Assign in Inspector
public MyScript myScript;
...

var instance = (MyScript )Instantiate(myScript);
instance.DoStuff();
or

var instance = this.gameObject.AddComponent<MyScript>();
instance.DoStuff();

I can instantiate myNonMonoClass without issue and I can use Instantiate without a problem but when I explicitly call a Mono method inside myNonMonoClass like MonoBehaviour.Instantiate() I get a warning “The class defined in the script file named ‘myNonMonoClass’ is not derived from MonoBehaviour or ScriptableObject!”. I don’t know if that warning is anything to worry about though.

I understand a MonoBehaviour script is supposed to be a component of a gameObject the problem is that even if I add myMonoBehaviourClass to a gameObject I still can’t access its non static classes through the gameObject. It seems as though Unity doesn’t actually instantiate the classes when they’re added to gameObjects.

You might want to show your code!? Because I dont’t get it … in your first post you said that your class inherits from MonoBehavior and now it doesn’t inherit from it.

it simple
MonoBehavior : Instatiate/AddComponent/GetComponent to get a reference/instance to the object
ScriptableObject : use ScriptableObject.CreateInstance
“normal” c# class: use new keyword

I change the code to test it as a MonoBehaviour and as a NonMonoBehaviour. I can change the code to whatever it needs to be for testing purposes. In any case here are the two versions I am referring to:

//Non Mono Class
public class Social
{
    public void InstanSocial()
    {
        if (GameObject.FindGameObjectWithTag("social") == null)
        {
            GameObject socialPrefab;

            socialPrefab = new GameObject();

            if (socialPrefab != null)
            {
                socialPrefab = Resources.Load("Prefabs/Social") as GameObject;
                
                //I receive an warning on this line as stated before.
                MonoBehaviour.Instantiate(socialPrefab);

                return;
            }
            else
            {
                InstanSocial();
            }
        }
        else
        {
            return;
        }
    }
}
//Instantiate Non Mono Class
public class Contructor : MonoBehaviour
{
    public static Social social;

    public void SomeMethod()
    {
            social = new Social();

            social.InstanSocial();

         return;
    }
}

Here is the MonoClass

//MonoClass

public class Social : MonoBehaviour

{

    public static void InstanSocial()

    {

        if (GameObject.FindGameObjectWithTag("social") == null)

        {

            GameObject socialPrefab;

 

            socialPrefab = new GameObject();

 

            if (socialPrefab != null)

            {

                socialPrefab = Resources.Load("Prefabs/Social") as GameObject;

 

                Instantiate(socialPrefab);

 

                return;

            }

            else

            {

                InstanSocial();

            }

        }

        else

        {

            return;

        }

    }

}
public class Contructor : MonoBehaviour
{

    public static GameObject social;

    public void SomeMethod()
    {
            Social.InstanSocial();

            social = GameObject.FindGameObjectWithTag("social");

            //Here is where I cannot call the method from the game object even though it exists.  I have to call it directly from the Social                                
            //class.
            social.SomePublicNonStaticMethod();
    }
}

So the first method returns a warning of which I’m unsure if its a real issue. The second way I can’t call methods on the gameObject the has the class as a component.