Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andrew95434 · Sep 09, 2012 at 07:17 PM · monobehaviournewkeyword

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?
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew95434 · Sep 09, 2012 at 10:12 PM 0
Share

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

avatar image
0

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".)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew95434 · Sep 09, 2012 at 07:51 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges