You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed.

i have this code:

using UnityEngine;
using System.Collections;

public class Cards : MonoBehaviour {	
	
	public bool first = true;
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}	

	void OnMouseOver() { 
		
		if(Input.GetMouseButtonDown(0)){	
			this.transform.rotation = Quaternion.AngleAxis(30,Vector3.up);
			Cards cds = new Cards();
			if(first==true){				
				cds.firstCard = this.gameObject.tag;
				first=false;
			}
			else {
				if(this.gameObject.tag==cds.firstCard){
					print ("muy bien");
				}
				else {
					print ("intentalo nuevamente");
					GameObject h = GameObject.FindGameObjectWithTag(cds.firstCard);
					this.transform.rotation = Quaternion.AngleAxis(0,Vector3.up);
					h.transform.rotation = Quaternion.AngleAxis(0,Vector3.up);
				}
				first=true;
			}
		}
    }
	
	private string fc;
	public string firstCard
        {			
            get { return fc; }
            set { fc = value; }
        }	
}

i can start my game without any error, but when i click on the gameobject which has this script, console sends me an error saying this:

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()
Cards:.ctor()
Cards:OnMouseOver() (at Assets/Cards.cs:22)
UnityEngine.SendMouseEvents:DoSendMouseEvents()

there are 2 things i’m not pretty sure about the code. first, i put “this.gameObject.tag;” refering to the tag of the object which contains the script. the other thing is when i wrote “Cards cds = new Cards();”, i wrote that to call the string firstCard later. what am i doing bad?

The error pretty much explains everything. You can’t create a MonoBehaviour using “new”, you have to use AddComponent. If you don’t want to add a component, then don’t extend MonoBehaviour. If you’re trying to create new objects in the scene, use Instantiate along with prefabs. (You don’t need “this”, by the way…“this.transform.rotation” is the same as “transform.rotation”.)

It sounds like you’re trying to make a Memory game or something, where you need to select two cards. The way to do this is not to create new MonoBehaviours, but to have a controller class that handles all of the input for you. Think about it from a practical poiint of view: How should a card know what other cards have been selected?

I did something similar for LD, and you can view the source code here (ControlBlock.cs). Basically, you give the card prefab a collider, then use Camera.ScreenPointToRay() to capture clicks on a card’s collider to select it. You then store the selected card’s gameObject reference, and use it to do your checks right there in the controller script.