Monobehaviour warning for new keyword

//class name - Vectors()

#pragma strict
var target : Rigidbody;
private var Dir : Vector3;
Dir = transform.position;
private var px : Vector3=  Vector3(2,2,2);
function Update () 
{

if(Input.GetMouseButtonDown(0))
{
Dir.y = target.position.y + px.y;
target.position = Dir;
}
if(Input.GetMouseButtonDown(1))
{
Dir.y = target.position.y - px.y;
target.position = Dir;
}
}

//class name - SimpleMovement_circular

#pragma strict

private var z: float;
private var y :float;
var x : float = 5;
private var PosRef : Vectors = new Vectors();
var target : Rigidbody;
var rotationSpeed : float;
private var i : int = x;

function Awake() 
{

}

function Update () 
{
	for(i=0 ; i<x ; i++)
	{
	y = Mathf.Lerp( 0+i , i+1 , rotationSpeed*Time.deltaTime);
	z = Mathf.Lerp( 0+i , i+1 , rotationSpeed*Time.deltaTime);
	}
}

warning shown for the second script —

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()
Vectors:.ctor() (at Assets/Scripts/Vectors.js:1)

what could possibly be the problem and hoe could i create a reference to the script named Vectors In the other script…could the reference be setup in the awake or the start functions.

Hello,

In Unityscript, when a script is compiled, the contents of that script which are not in a class will be automatically placed in a class named after that of the file and will automatically inherit from MonoBehaviour. MonoBehaviour objects cannot be constructed by the usual means, because of how they work with the Unity Engine.

If you want to make a class that doesn’t inherit from MonoBehaviour you should learn how the class keyword works :slight_smile:

Hope this helps,
Benproductions1

Sure if both scripts are already in the scene, attached to different objects you can simply drag the gameobject that has the “Vectors” script attached onto the variable PosRef in the inspector. For this the variable has to be public!

If you create the objects dynamically (so both or one of them are Instantiated at runtime) you can setup the reference in Start. There are many ways to get a reference to another object. Keep in mind that every MonoBehaviour has to be attached to a GameObject.

See Accessing Other GameObjects for various ways to access scripts / components on the own gameobject or others.

We can’t give you a more detailed answer without knowing how your scene hierarchy looks like and which script is attached to what.