- Home /
Help with variables
Hello!
I have this script
function OnTriggerEnter (other : Collider) { if(used == false) { // Find the child named "ScoreHolder". // On the OtherScript attached to it, set score ++. transform.Find("ScoreHolder").GetComponent(Counter).score ++; } }
function OnTriggerExit (other : Collider) { if(used == false) { used = true; } Destroy(destroyedobj); }
that every time the "player" walk into a "box", the other script
var score : int = 0;
function Update () {
if(score == 5) { //something happens score = 0; } }
change the score,but the object with the second script must be a child to the first one. The problem is that I can't have the second script a child to every "box" that the "player" walks into...
What can I do? Please help...
Answer by brunopava · Aug 23, 2012 at 08:20 PM
Make a script like this:
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "Player")
{
other.gameObject.SendMessage ("ApplyScore", incomingScoreValue);
GameObject.Destroy(gameObject);
}
}
And attach is to the boxes.
Then on any of the player's scripts you put a function like this:
function ApplyScore(incomingScore:int)
{
//do whatever you want with the score
}
it has an error "$$anonymous$$ identifier: 'inco$$anonymous$$gScoreValue'."
you are suposed to declare a variable with that name, or put a float value there.
becouse I'm noob, a var in the first script like that? var inco$$anonymous$$gScoreValue : float = 0;
exactly! you got it right with the variable, of course remember that you can call your variables anything you dont have to do what others do! :D
Answer by Kryptos · Aug 23, 2012 at 05:23 PM
Put the second script in a child of the player object and the first one on the player.
Your answer
Follow this Question
Related Questions
Access other variables please help :( 1 Answer
Is it possible to create new variables by incrementing? 1 Answer
Trying to access a variable in a different script on a different game object. 1 Answer
Accessing a js static var from a c# script 1 Answer
One of my scripts can't find the variable from this script 1 Answer