x


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 avariable', value' ormethod group' was expected

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

more ▼

asked Nov 20 '11 at 07:33 PM

maggot gravatar image

maggot
190 26 28 36

I don't see how what Unity tells you doesn't solve your problem. A MonoBehaviour is something to be attached to a Game Object.

Nov 20 '11 at 08:15 PM Jessy

missileControls.cs is a script component added to my missile prefab player001controls.cs is a script component added to my player GameObject

Nov 20 '11 at 08:41 PM maggot

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 = Instantiate(player001PreFab, Vector3.zero, Quaternion.identity);

Nov 20 '11 at 08:54 PM Holon777

One of the basic rules of Unity is that you don't use constructors with MonoBehaviours. This means you don't use the "new" operator. You attach scripts to GameObjects and reference them with public variables or GetComponent.

That has got to be one of the most descriptive and helpful compiler messages you're ever going to see (Unity rocks)!

Nov 20 '11 at 08:56 PM jahroy
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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; 
more ▼

answered Nov 25 '11 at 12:12 PM

maggot gravatar image

maggot
190 26 28 36

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 21 '11 at 12:13 AM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

(comments are locked)
10|3000 characters needed characters left

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));

more ▼

answered Nov 20 '11 at 11:36 PM

Holon777 gravatar image

Holon777
3 4

(comments are locked)
10|3000 characters needed characters left

I recommend that you start by reading this:

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

more ▼

answered Nov 20 '11 at 09:02 PM

jahroy gravatar image

jahroy
3.2k 14 18 42

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3418
x2029
x151
x140
x131

asked: Nov 20 '11 at 07:33 PM

Seen: 4595 times

Last Updated: Nov 25 '11 at 11:43 PM