Error trying to createMonoBehaviour using the 'new' keyword in cSharp script

I’m trying to read a public float called shootForce contained in a monoBehaviour class called player001controls using a monoBehaviour script called missileControls with cSharp.

player001controls.cs (extract)

	private float pShootForce;

public float shootForce // The force at which missiles travel in m/s
{
	get
    {
        return pShootForce;
    }
    set
    {
        pShootForce = value;
    }

}

missileControls.cs (extract)

player001controls missileControl = new player001controls();

This executes, but when I fire a missile I get this error :

You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
player001controls:.ctor()
moveMissiles:FixedUpdate() (at Assets/Scripts/moveMissiles.cs:9)

Removing the ‘new’ from

player001controls missileControl = new player001controls();

gives the error

Assets/Scripts/moveMissiles.cs(9,60): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

I want to be able to access the float shootForce at runtime, read from player001controls (or any other script).

To create an instance of a MonoBehaviour class at runtime you have to use AddComponent because all components can’t exist on it’s own. They always need to be attached to a GameObject. If you want to create a script instance on the same GameObject where this script is attached to just use:

// C#
gameObject.AddComponent<player001controls>();

Ohh a little sidenote: classnames always should start with a capital letter. It get’s very very difficult to read code where you mix typenames with variable names. That are some basic programming conventions.

I think you you make a prefab out of the player001controls and Instantiate it, you’ll be good to go.

public GameObject player001PreFab; // set this with inspector

player001controls missileControl;

GameObject gObj = Instantiate(player001PreFab, Vector3.zero, Quaternion.identity);

missileControl = (player001controls)gObj.GetComponent(typeof(player001controls));

I recommend that you start by reading this:

The above link explains some of the basic concepts of how scripts interact in Unity.

OK, heres my solution (final)

Declaration :

public class Bullet
{

    private float _shootForce;

    public float shootForce
{
	get
	{
		_shootForce = 1f;
		return _shootForce;
	}
	set
	{
		_shootForce = value;
	}
}

}

Invocation :

	Bullet bullet = new Bullet();

Execution :

	rigidbody.velocity = transform.up * bullet.shootForce;