Adding GameObject to non-MonoBehaviour class

Hello,

I am trying to add a reference to game object in my hierarchy from a custom class. But I get an exception.

Here is an example :

class Weapon{
public GameObject weaponModel;

//other functions

}

I then drag game object from hierarchy view to “Weapon Model” field in the script.

When trying to Instantiate a Weapon object, GameObject.Instantiate() throws the following exception

ArgumentException: The prefab you want to instantiate is null.

What can I do? is it necessary for my class to inherit MonoBehaviour and be attached to an object in the scene?

You can’t Instantiate a Weapon (though you could instantiate a weaponModel). What are you actually trying to do? Sounds to me like you want to create a prefab with a “Weapon” script attached, but you’re not.

Also, it’s not GameObject.Instantiate(), it’s Object.Instantiate(). No point in climbing down that tree.

Sorry for the mistake.
Actually, what I am trying to do is calling Object.Instantiate(weaponModel) somewhere in Weapon class (but not in constructor). Weapon class is not a child of MonoBehaviour, and I don’t want to attach it to a game object.
In fact, when I point to Weapon class in project hierarchy, the inspector shows the public field weaponModel, and I drag my prefab there. But initialization doesn’t work at runtime

Also, sorry for not saying before, welcome to the forum! :smiley:

I think it’s best that you upload a .unitypackage. Without at least further code, I can’t see where the problem is.

I’m probably misunderstanding this too, but is this what you were driving at?

var myWeapon : Weapon = new Weapon();
myWeapon.weaponModel = Instantiate(yourGunModelPrefab, …position, etc.)

EDIT: yeah, I think I’m way off base here. This is trying to do all this in the Unity Editor inspector, eh?

I was suggesting you upload a unitypackage with only the relevant items. What you’ve shown so far does not describe the flow of your code, or what is serialized. For example, where did you get yourGunModelPrefab? This line should be where the problem lies.

I also don’t know what you’re asking here.

lol Jessy, I’m flattered, but I’m not Yasser J :stuck_out_tongue:

EDIT: maybe it’s a sign I should finally set an avatar, eh?

Wow. :stuck_out_tongue:

Yeah, avatar = highly recommended by me. This isn’t the first time I’ve done this, and it was with another one of you faceless masses before, too! :hushed:

Thanks for efforts. I managed to make a work around. I made my Weapon class inherit from MonoBehaviour, and attached my Weapon’s subclass to empty game object. It seems that scripts cannot be assigned components from inspector unless they are actually in scene hierarchy and attached to some game object ← Correct me if this is wrong

I’m not sure, but I wouldn’t be surprised; it probably wouldn’t know how to construct an instance. Or more likely, you’d be trying to assign the type of the script and not an instance of itself.

I think he meant something like this:

GameObject go = (GameObject)GameObject.Instantiate(GameObject.Find(“gameCube1”), new Vector3(0,0,0), Quaternion.identity);

Where gameCube1 is in the scene I believe. I use this exact line in a non-Inheriting Monobehavior script (C#).