0

I have two scripts (for example Script1(for creating data) and Script2(for working with data)) in one object, Main Camera.

Script1:

namespace something
{
  public class Data
  {
    public GameObject obj;
  }

  [ExecuteInEditMode]
  public class Create: MonoBehaviour
  {
    Data[] data;
    void Start()
    {
      data = new Data[NumData()]; //NumData() = int (e.g. 5)
      CallFunction();
    }
    void CallFunction()
    {
      foreach(Transform obj in object)
      {
        for(int id = 0; id < NumData(); id++)
        {
           data[id].obj = object.gameObject;
        }
      }
//... etc

Script2:

using something;

[ExecuteInEditMode]
public class WorkWithData: MonoBehaviour
{
  Data[] data;
  void Update()
  {
    data = new Data[NumData()];
    for (int id = 0; id < NumData(); id++)
    {
      data[id] = gameObject.GetComponent<Data>();
    }
    CallFunction();
  }

  void CallFunction()
  {
    //just getting information about data[].obj
//...

But Script2 gives an error:

ArgumentException: GetComponent requires that the requested component 'Data' derives from MonoBehaviour or Component or is an interface.

I know it says I must write:

public Class Data : MonoBehaviour

In Script1.

But when I write it, Script1 gives a warning:

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

When I put script2 into script1, like a new class, it works correctly. But I want it to be separated into more scripts.

I´m a newbie in scripting and programming, so I'm glad for any advice.

  • You left an important part of the error out. Here it is: *"This is not allowed. MonoBehaviours can only be added using AddComponent()."*. That says what you need to use. See duplicate if you are still confused. – Programmer Feb 02 '18 at 10:43
  • Thanks for the answer. I'm stupid. Only one thing, what I must do in Script1 is rename: Data[] data; to public static Data[] data; And in Script2 use using something and CallFunction.data[]... Now it works! – user9304087 Feb 02 '18 at 11:36

0 Answers0