Well you are doing
var cube1 = new Cube1(); which is how you would make an object in C#, but since it is Unity, and this is a MonoBehaviour, you are not supposed to make it like that.
Instead, make a new
GameObject and attach the
Cube1 script to it.
// make a new gameobject.
GameObject gameObject = new GameObject();
// adding a SpriteRenderer as your `Cube1` will try to access it.
// -- please also see the bottom note of the answer. --
// attach a Cube1 script to it and store the reference to it in a var.
Cube1 cube = gameObject.AddComponent<Cube1>();
// call the function on that cube.
Sidenote, if you don't make an object in the scene that holds the ClockController, it won't execute the
When you already have an existing
GameObject (instead of making a new one) that has a script you want to access, you can use
gameObject.GetComponent<Cube1>() , store it in a variable (if necessary) and access it that way. You just need to get that specific
GameObject to begin with.
Extra info on component dependencies:
As pointed out by @DMGregory,
cube.Rectangle() will try to find/access a
SpriteRenderer on the
cube object, which would have to be added first. You could add
[RequireComponent(typeof(SpriteRenderer))] which means that whenever you add a
Cube1 script to a
GameObject, the required component is added aswell if it is not already there.
I'd suggest to take a look at the docs: https://docs.unity3d.com/ScriptReference/RequireComponent.html
Alternatives would probably be making sure in your Cube1 script that at some point a SpriteRenderer is attached, but that's all going off-topic I think.