So I have a game where a player places blocks in a 2D world. This requires some sort of counter so I put that counter in one script:
#pragma strict
public var cam : GameObject;
public var block : GameObject;
public var amount = 500;
function Start () {
transform.FindChild("Amount").GetComponent.<UI.Text>().text = amount.ToString();
}
function Update () {
//This is just for graphics. Ignore it.
var rotationOfset = cam.GetComponent(RaycastDetector).rotationOffset;
var temp = transform.root.eulerAngles;
temp.z = -rotationOfset;
transform.rotation = Quaternion.Euler(temp);
}
function OnClick () {
//This sets 3 vars in another script when clicked
cam.GetComponent(RaycastDetector).currentBlock = block;
cam.GetComponent(RaycastDetector).textBox = transform.FindChild("Amount").GetComponent.<UI.Text>();
cam.GetComponent(RaycastDetector).currentScript = this;
}
This script holds the amount of a type of block a user has. This script is held in a variable used by another script (simplified so it isn’t too messy):
#pragma strict
//Bunch of variables
//I simplified the script to the part that's actually important so most of these won't be used
public var currentBlock : GameObject;
public var rotationOffset : float;
public var currentHand : GameObject;
public var hands : GameObject;
public var textBox : UI.Text;
public var clickScript : MonoBehaviour;
function Start () {
}
function Update () {
//Cast a ray
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Input.GetMouseButtonDown (0) && amount.amount != 0) {
if (Physics.Raycast(ray,hit) ) {
if (hit.collider.tag == "Detector") {
//Update the amount of blocks held by a player
amount.amount = amount.amount - 1;
//Update text
textBox.text = amount.amount.ToString();
//Create actual block
var clone : GameObject;
var vec = currentBlock.transform.rotation.eulerAngles;
vec.x = vec.x + rotationOffset;
clone = Instantiate(currentBlock, hit.collider.gameObject.transform.position, Quaternion.Euler(vec));
}
}
}
}
The problem is that Unity doesn’t allow me to enter play mode since the counter is accessed by typing “clickScript.amount” but “amount” is not a part of Monobehaviour. I can’t do this manually in the script either since the script can’t be compiled. How can I fix this?