Call a function in another script, which instantiates an object?

Forgive me my newbness, I know not what to do.

I’ve read the answers and documentation on calling a function from another script without monobehavior and I can do that. My problem is one level more complex than that. I’m hoping you can help.

I have a little cube that floats across a plane, spawning one of six object prefabs. The actions (movement, and 1-6 number keys which correspond to the object to be spawned) are controlled by the script “Player1”. I also have a script called “SpawnUnit”. This script is fed three values by “Player1” which determine the type of object to be instantiated. The “SpawnUnit” script is not attached to an object.

Briefly, the problem I’m running into is that if I remove monobehavior from the SpawnUnit script, I can call that script from Player1, but if I remove monobehavior from SpawnUnit the spawn function can’t call “Instantiate”.

How do I call a function in a different script, without using monobehavior for the called script, when I need monobehavior in the script in order for that script to use instantiate?

Thanks for the help!

Have your SpawnUnit class call Object.Instantiate() instead of Instantiate().

Instantiate() is a public static method of the class UnityEngine.Object, which is the base type of GameObjects and MonoBehaviours. Since it’s a public static method, any class (even ones that aren’t MonoBehaviours) can call it with Object.Instantiate() as long as the script has “using UnityEngine;”, and it has a reference to a prefab or an existing object to make a copy of.

GameObject.Instantiate()

Instantiate is a static function, which can be called outside an instance of the class when qualified with the class type before the function call.