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

Hi all.

I had to change a variable in a script from another script. So I added a ref to the calling script (Asteroid.cs) and tried to get to the variable in the other script (CodeGlobal.cs):

CodeGlobal cg;

    private void OnCollisionEnter(Collision collision)
    {
        // ...
       
        astroN = Random.Range(0f, 2f);

        astroN = Mathf.Round(astroN);

        codeG.GetComponent<CodeGlobal>().astroN = astroN;

But I can’t assign the object/script (CodeGlobal.cs) in the editor. The object list is empty:

What am I doing wrong?

Thanks!

I’m struggling to understand your question. Your post title does not seem to be relevant. Can you clarify what you’re asking exactly?

Also, are you sure you don’t have any compiler errors still active??

Oh i’m so sorry, I was going to ask another question, but typed this one and forgot to change the title, and hit Post!

No compiler errors. Which part of the question is not clear please?

Basically, I’m trying to communicate between two scripts, but i don’t see the Object which is attached to CodeGlobal.cs. The list is empty.

P.S. Video: