Search Unity

boolean giving monobehavior error/now about monobehavior error in general

Discussion in 'Scripting' started by gibberingmouther, Jul 26, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    the error is the classic and familiar:
    "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"

    but it is being given on a boolean: bool enemy_over = false;

    there are two booleans directly above this one that do not give an error. the only difference is that this boolean is used a couple times in the script - twice as all or part of the condition of an if statement, and inside the body of an if statement. actually now that i'm looking it over, the other two booleans clicked and enemy_clicked are also used in the script: in the body of if statements and as if statement conditionals. so i had previously thought it was how it was used that made enemy_over separate from the other two booleans, but they are used in the same way as i indicated above, so i am lost on this one. i'll post some of the relevant pieces of code below.

    i have already tried renaming enemy_over, but it gives the same error, so it is not a case of a corrupted variable. i guess i could ignore it since it doesn't prevent me from running my game but i would really hate to do that.

    code with enemy_over in it:
    Code (csharp):
    1.  
    2. if (enemy_over == false)
    3.                 {
    4.                     Debug.Log("!Enemy_Over");
    5.                     clicked = true;
    6.                     enemy_clicked = false;
    7.                     targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.                 }
    9.                 else if (enemy_over == true && InAttackRange(Input.mousePosition, 10))
    10.                 {
    11.                     Debug.Log("Enemy_Clicked");
    12.                     enemy_clicked = true;
    13.                     clicked = false;
    14.                     //enemy_over = false;
    15.                 }
    16.  
    and
    Code (csharp):
    1.  
    2. if (hit.collider != null && hit.collider.tag == "Enemy")
    3.             {
    4.                 //Debug.Log("enemy_over");
    5.                 enemy_over = true;
    6.             }
    7.  
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Can you show the whole script?

    There is not enough context to see what the issue is. The boolean comparison is not the issue, something is leading you astray.
     
  3. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    edit: as i think of it, i guess if i can't solve it i can leave it be since it doesn't impede the execution of the program ... but should give a shot at understanding what's going on. this is the field i want to work in after all.

    edit2: deleted some code
     
    Last edited: Jul 27, 2017
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I can't see anything there that wold cause such an issue..

    What's the actual error? Can you provide that too?
     
    gibberingmouther likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Code (CSharp):
    1.  
    2. attack_skill_hits attack = new attack_skill_hits();
    3. attack_skill_evades defend = new attack_skill_evades();
    4. swinging_thrusting damage = new swinging_thrusting();
    5.  
    What are these? Do they inherit from monobehavior?

    You're basically looking for any place you are calling "new" at. The vector3 calls are fine. So that leaves these.
     
    gibberingmouther likes this.
  6. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    those also give an error, but i was about to fix them, i just haven't gotten around to it yet because i'm stuck on these other things. those make sense. it's line 47 that's giving the error for some reason.

    edit: while we're on the subject of monobehaviors, is this a good alternative by the way? :
    GameObject myObject = new GameObject();
    BaseCharacterClass class = myObject.AddComponent<BaseMageClass>();

    or in my code it would be, for example: attack_skill_hits class = myObject.AddComponent<attack_skill_hits>();

    i think the original example used inheritance which confused me.

    edit2: would have run into this sooner or later. this does not work:
    GameObject attack = new GameObject();
    attack_skill_hits class = attack.AddComponent<attack_skill_hits>();
     
    Last edited: Jul 26, 2017
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    "class" is a reserved name. So you can't use that as a variable name. Use something else.
     
    gibberingmouther likes this.
  8. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    okay, got it working. here's where i found my code: https://answers.unity3d.com/questions/958213/create-a-new-monobehavior-not-allowed.html

    i wish it were possible to access the comments posted by users in older threads. that's a lot of useful information lost.

    thanks Brathnann! still have the issue of the boolean giving the monobehavior error, but it's not a show stopper. still will wait and see if anyone knows what's going on there.
     
  9. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Those bool's are no causing any issue - the issue is elsewhere. As pointed out earlier, the only lines that can cause this error are ones that call operator 'new'. There is no way a boolean could produce that error. Either the error output is wrong or it's being misunderstood.

    Can you paste the actual error in its entirety?

    These three lines look to be the real cause:
    Code (csharp):
    1. attack_skill_hits attack = new attack_skill_hits();
    2. attack_skill_evades defend = new attack_skill_evades();
    3. swinging_thrusting damage = new swinging_thrusting();
    4.  
    If these inherent from MonoBehaviour (in any way) then these lines will cause the error you describe.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If you changed all those new into AddComponent, then that will fix that error. If you are still getting that "new" error, and it points to a bool line, then that is something else. As mentioned in another post I saw, someone mentioned that if line endings are off, sometime errors will point to the wrong lines. But at this point, you shouldn't be seeing that new warning anymore.