You are trying to create a MonoBehaviour using the 'new' keyword

Hello guys,

I’m having trouble with my code and I can not solve it.

The error is the following:

Below also follow both classes involved in the error:

Character.cs:

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour 
{	
	void Update () 
	{
		characterMoving();
		characterAttack();
	}
	
	public void characterAttack() 
	{
		IAttackBehaviour a;
		if (Input.GetKeyDown(KeyCode.Alpha1))
		{
			a = new GunAttack();
			a.attack();
		} else
		if (Input.GetKeyDown(KeyCode.Alpha2))
		{
			a = new SwordAttack();
			a.attack();
		} 
	}
	
	public void characterMoving() 
	{
		Movement m = new Movement();
		m.running();
	}
}

Movement.cs:

using UnityEngine;
using System.Collections;

public class Movement : Character {

	private float runSpeed = 10.0f;
	private float jumpGravity = 10.0f;
	private float jumpPower = 20.0f;
	private Vector3 moveDirection = new Vector3(1,0,0);
	
	public void running() 
	{
		CharacterController c = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();
		moveDirection = c.transform.TransformDirection(moveDirection);
		moveDirection *= runSpeed;
		if ((Input.GetKeyDown(KeyCode.Space))(c.isGrounded)) moveDirection.y = jumpPower;
		moveDirection.y -= jumpGravity * Time.deltaTime;
		c.Move(moveDirection * Time.deltaTime);
	}
}

Thanks in advance to those who respond.

Like it says, you can’t instantiate a MonoBehavior class with the ‘new’ keyword.

Instead, add it as another component on the object and then get a reference to it in your Character script.

That still will cause problems, though, because you’ve inherited from Character, and so all that code will be executing again.

Thanks for replying,

I made the following change, the error disappeared, but the character does not jump gently:

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour
{	
	void Update () 
	{
		characterMoving();
		characterAttack();
	}
	
	public void characterAttack() 
	{
		IAttackBehaviour a;
		if (Input.GetKeyDown(KeyCode.Alpha1))
		{
			a = new GunAttack();
			a.attack();
		} else
		if (Input.GetKeyDown(KeyCode.Alpha2))
		{
			a = new SwordAttack();
			a.attack();
		} 
	}
	
	public void characterMoving() 
	{
		IMovementBehaviour m = new Movement();
		m.running();
	}
}
public interface IMovementBehaviour
{
	void running();	
}
using UnityEngine;
using System.Collections;

public class Movement : IMovementBehaviour {

	private float runSpeed = 1.0f;
	private float jumpGravity = 20.0f;
	private float jumpPower = 10.0f;
	private Vector3 moveDirection = new Vector3(1,0,0);
	
	public void running() 
	{
		CharacterController c = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();
		moveDirection = c.transform.TransformDirection(moveDirection);
		moveDirection *= runSpeed;
		if ((Input.GetKeyDown(KeyCode.Space))(c.isGrounded)) moveDirection.y = jumpPower;
		moveDirection.y -= jumpGravity * Time.deltaTime;
		c.Move(moveDirection * Time.deltaTime);
	}
}

Any idea of what problem I am facing?

Perhaps you should adjust your jump power and gravity variables?

You are creating a new Movement object on each Update() call - this is doing two things:

  1. On each frame, you are creating a new instance of Movement object, which is unnecessary and will impact memory usage/performance
  2. Since each frame a new Movement object is created, each time you run m.running(), it’ll always be working with the default values for each of the member in Movement class (for example, moveDirection will always appear to be 1,0,0 because you are always dealing with a new copy of Movement object).

Keep a single reference to the Movement object in your Character class instead of creating a new one each time.