- Home /
[JS] Getting an Error In My Money System Script
So I tried to make a very simple script where if the cube "Money" collided with "Player" (My FP controller), The variable TheMoney would increase by 50... BUT I'm getting an error saying that Money is not a member of UnityEngine.GameObject This script is attached to the cube called "Money" and the cube has a Box Collider and a Rigid Body (BTW this is Javascript)
MY CODE:
#pragma strict
var col : Collision;
var MoneyAmount : int = 50;
var CurrentMoney : int = 0;
function OnCollisionEnter (col : Collision) {
if (col.gameObject.Money == "Player") {
CurrentMoney = CurrentMoney + MoneyAmount;
}
}
Try this :
#pragma strict
var $$anonymous$$oneyAmount : int = 50;
var Current$$anonymous$$oney : int = 0;
function OnCollisionEnter (col : Collision) {
if (col.gameObject.tag == "Player") {
Current$$anonymous$$oney = Current$$anonymous$$oney + $$anonymous$$oneyAmount;
}
}
$$anonymous$$ake sure your player is tagged as "Player" !!!
So I did what you said and Im not longer getting the error, but the variable "$$anonymous$$oney" is not changing
Have you tagged your player?
Tags are not the same as names!
I DID IT!!!!!!!!!!!!!!!! YAY!!!!!!!!!!!!!!! I$$anonymous$$ SO HAPPY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Answer by JSierraAKAMC · Nov 25, 2014 at 04:05 AM
Well, in order to use OnTriggerEnter, the collider for the money has to be a trigger. In order to use a rigidbody with a trigger collider, I would add an empty GameObject and make it a child of the money object, then add a Box Collider to it. Make that collider a trigger and set the size to about a half of a unit larger than your money object (so that the player can enter it). Here's a picture since these instructions are kind of confusing: 
Your answer
Follow this Question
Related Questions
Collision Killing CPU/FPS/Performance - Suggestions?! 0 Answers
Strange Collisions (...as there should be none!) 0 Answers
Having difficulties making a block be a death trigger for a ball. 2 Answers
Unity Collision Returns child instead of parent object 2 Answers
Physic based golf game - ball bouncing off the connection of colliders on flat surface 2 Answers