Must derive from MonoBehaviour or ScriptableObject

Hello everyone. I got this compiler warning which is driving me nuts because it just wont leave.

The class defined in the script file named 'VectorGrid' is not derived from MonoBehaviour or ScriptableObject!

This kind of confuses my because I got more classes that are not derived from any of these.

Edit: This was not the root of my “can not build for IOS” problem. Though it confuses me. I could not build the iOS app cause I didnt read the Vectrosity Documentation (using source code now instead of dll and it works)

Here is the class that causes the error:

public class VectorGrid{

	private float strength;
	private float height;
	private float lineOffset;
	private int lineCount;

	public VectorLine line;
	public VectorLine smallLines;
	
	public VectorGrid () {
		strength = 0.5f;
		height = 0.5f;
		lineOffset = 10f;
		lineCount = 10;
		
		float xOffset = -((lineCount-1) * lineOffset);
		float zOffset = -((lineCount-1) * lineOffset);
		Vector3[] arr = new Vector3[lineCount*4];
		
		// z axis
		for(int i = 0; i < lineCount*2 ; i+=2){
			arr _= new Vector3(i*lineOffset+xOffset, height, zOffset);_

_ arr[i+1] = new Vector3(i*lineOffset+xOffset, height, (lineCount-1)lineOffset2+zOffset);_

  •  }*
    
  •  // x axis*
    

_ for(int i = 0; i < lineCount2 ; i+=2){_
_ arr[i+20] = new Vector3(xOffset, height, i
lineOffset+zOffset);_
_ arr[i+20+1] = new Vector3((lineCount-1)lineOffset2+xOffset, height, i*lineOffset+zOffset);_

  •  }*
    
  •  line = new VectorLine("Line", arr, Color.gray, null, strength);*
    
  • }*

  • public void alignTo(GameObject go){*

  •  GameObject line = GameObject.Find("Vector Line") as GameObject;*
    
  •  if(line == null)*
    
  •  	throw new Exception("VectorGrid: Line not found on scene");*
    
  •  line.transform.parent = go.transform.GetChild(0);*
    
  •  line.transform.position = go.transform.TransformPoint(Vector3.zero);*
    
  •  line.transform.rotation = go.transform.GetChild(0).rotation;*
    
  •  line.transform.Rotate(Vector3.right, 90f);*
    
  • }*
    }
    I appreciate any answer, comment or thought

Do you have another component called VectorGrid (in Plugins etc) perhaps not written by you but a plugin? Sounds like it's getting confused.

Nope just that single class.

Where do instance(s) of this class exist? (For example, as a variable inside a MonoBehaviour class.) Was this class ever a MonoBehaviour that was attached to a game object? Is any class trying to run SendMessage or BroadcastMessage on it?

Just one instance of this class exist in an MonoBehaviour that is on the same object as another monobehaviour that sends messages to all scripts on that object. Is that a problem?

GetChild() is an undocumented, probably unsupported feature. I think they removed it because children are unordered. Can you rewrite alignTo() without using GetChild()?

3 Answers

3

At the end of your class definition you need to add :

public class VectorGrid : MonoBehavior {
///…
}

or if you don’t want to derive from that, you need to remove anything you’ve declared that uses the MonoBehaviour class.

Doesn't look like he's using anything that requires it to be a MonoBehaviour to me...

What is Vectorline?

VectorLine is a class from the Vectorsity pack to draw Lines. And yes iam not using anything from monobehaviour (if you think of Update or something like that) but even though I could have a function called Update but it won't be called every frame.

Ok. I removed the Transform.GetChild(int) method in my AlignTo method and the Warning seems to be gone.

Thanks TonyLi

I was saved by the following advice: “Remove the line “using UnityEditor” and all will be well ;)”

Perhaps you can be too :wink:

Either that or the script being in an "Editor" folder