- Home /
guitext count up when collide
what am i doing wrong here? what im trying to do is when this object collides with the object with the tag gold, the goldcouter counts +1
var goldtext : GUIText;
var goldcounter : int = 0;
function OnCollisionEnter (geraakt : Collision) {
if (geraakt.gameObject.tag == "gold") goldcounter++;
var goldtext = guiText.text ;
guiText.text = "Gold: " + goldcounter;
}
there is nothing happening just says the stock GUI text on the screen.
keep in mind that im new to javascript
Comment
Answer by oliver-jones · Sep 25, 2013 at 06:05 PM
Try this:
function OnCollisionEnter (geraakt : Collision) {
if(geraakt.collider.gameObject.tag == "gold"){
goldcounter++;
}
goldText.text = "Gold: " + goldcounter;
}
this doesnt work becouse the script isnt attached to the guitext object. so i think i need to call for the guitext object, thats why i did the line var goldtext : GUIText; or is this more of a job for getcomponent?
here is the whole script. its attached to the player.
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
function Update () {
var controller : CharacterController = GetComponen (CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("$$anonymous$$ouse X") * rotateSpeed, 0);
// $$anonymous$$ove forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var right : Vector3 = transform.TransformDirection(Vector3.right);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
var curSpeed1 : float = speed * Input.GetAxis ("Horizontal");
controller.Simple$$anonymous$$ove(forward * curSpeed);
controller.Simple$$anonymous$$ove(right * curSpeed1);
Screen.showCursor = false;
}
var goldtext : GUIText;
var goldcounter : int = 0;
function OnCollisionEnter (geraakt : Collision) {
var goldtext = guiText.text;
if (geraakt.gameObject.tag == "gold");
goldcounter++;
guiText.text = "Gold: " + goldcounter;
}
@script RequireComponent(CharacterController)
Your answer
