Variable Rerferring

The title says it all! I looked at a few answers but… It didn’t seem to work. I’ll post up my two scripts

What I’m trying to do is add 15 to my counter (And I am struggling with making a GUI Text appear on the screen, so if anyone knows how to do that, that be amazing!!) but I’m really uncertain how to do that! When I have it in the 2nd script, it keeps adding 15 until the item is destroyed…

#pragma strict

var myGUITextObject : GUIText;
var matchBox = gameObject.GetComponent(MatchScrip).MatchCounter = 0;
var matchCount = "Acquired 15 matches.";
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;

					

function Start () {

}

function Update () {

if (Input.GetMouseButtonDown(0)){       
					
			if (Physics.Raycast(ray, hit, 100)){ // 1000 or Mathf.Infinity should be enough !
				// what did the raycast hit ?
				Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
				Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );

				if(hit.collider.gameObject.tag == "match")
				{       
			
					Destroy(hit.collider.gameObject, 3);
					MatchCounter += 15;
					}
			}
		}

NEXT SCRIPT

#pragma strict

public var MatchCounter = 0;

function Update () {
	
	if(MatchCounter >= 1){
		if(Input.GetKeyDown('f')){
			MatchCounter--;
			if(light.enabled == false)
				light.enabled = true;
				
		
			else light.enabled = false;
			}
		}
		
}

1 Answer

1

I would refer you to this answer on StackOverflow -javascript variable reference/alias

In javascript primitive types such as integers and strings are passed by value whereas objects are passed by reference. So in order to achieve this you need to use an object:

// declare an object with property x
var obj = { x: 1 };
var anotherObj = obj;
anotherObj.x++;
alert(obj.x); // displays 2