A
A
aeaeae12021-05-11 04:09:36
C++ / C#
aeaeae1, 2021-05-11 04:09:36

Shows an error in Unity what's the problem?

What's the problem Unity throws the following error
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()
CoreClient:.ctor() (at Assets/Code/CoreClient.cs:36)
AnimatorController:.ctor() (at Assets/ Code/AnimatorController.cs:9)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorController : MonoBehaviour
{
  
    private Animator anim;
    private CoreClient core = new CoreClient();

    void Start()
    {
        //try
        //{
        //    StartServer();
        //}
        //catch (Exception e)
        //{
        //    Debug.Log("<color=red>ERROR: </color> " + e);
        //}
       
        anim = gameObject.GetComponent<Animator>();
        core = gameObject.GetComponent<CoreClient>();
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-05-11
@GavriKos

Well, read what is written in the error. In plain text - do not create MonoBehaviour via New

V
Vitaly Kachan, 2021-05-12
@MANAB

CoreClient core = new CoreClient(); replace with CoreClient core = null;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question