- Home /
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:
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?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
Answer by DesiQ · Sep 09, 2012 at 08:38 PM
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.
i didn't understand haha, but this is because i don't know much about unity, i will study more about colliders and other unity stuff. thanks anyway for your answer. can you explain a little more about it? my mail is andrew95434@gmail.com
Answer by Eric5h5 · Sep 09, 2012 at 07:33 PM
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".)
i'm new in unity, so i didn't undertand very much what you said. waht i want to do is call a string which i defined out of the void On$$anonymous$$ouseOver, so when i give a value to the string, it get stored in a "global" variable, and then i can use the same value in the "else" clause.
Your answer
Follow this Question
Related Questions
Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 Answers
Can someone tell me whats the difference between these? 1 Answer
MonoBehaviours can only be added using AddComponent(). Alternatively. 1 Answer
Unity - UI Dropdown uses constructor for DropDownItem 0 Answers
Unity 2D rotation speeds problem 1 Answer