- Home /
Collecting x amount of items triggers event?
So, i have a points and pick up script, the things collected have the tag of Coins. When i collect x amount of coins i want to trigger an event such as when character collects 10 'coins', the wall of cubes disappears? I have no idea how to do this.
My pick up code is:
var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;
function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin")
{ Debug.Log("Other object is a coin"); score += 1; scoreText = "Score: " + score; Debug.Log("Score is now " + score); Destroy(other.gameObject); } else { score +=5; scoreText = "Score: " + score; Debug.Log("Score is now " +score); Destroy(other.gameObject); } }
function OnGUI () { GUI.skin = mySkin; GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }
Answer by karl_ · May 01, 2011 at 03:55 PM
Place the score check at the end of your OnTriggerEnter function, and set the object you'd like to destroy in the inspector.
var wall : GameObject;
// Check if over X amount and if so destroy the object set as 'wall' if(score >= 10) Destroy(wall);
I've just tried, that and it isn't destroying the item defined as the wall.
Answer by GesterX · May 01, 2011 at 03:32 PM
You must have a function to "pick up a coin". By your "points" I assume that every time a coin is picked up a point is added.
So in your function which "adds a point" you would want to do a check to see how many total points the player has. In this code I'm assuming your player coin count is a var called "playerCoins":
function pickUpCoin()
{
//all your coin picking up code
if (playerCoins == 10)
{
DoSomething(); //call the function to do something (destroy wall etc)
}
}
If you want something more in depth then edit the question and add your current code.
EDIT:
function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin")
{
Debug.Log("Other object is a coin");
score += 1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
else
{
score +=5;
scoreText = "Score: " + score;
Debug.Log("Score is now " +score);
Destroy(other.gameObject);
}
if (score >= 10) { //do something } }
Sorry i'm new to all of this, where would i put this code? in my pick up script or a new script?
I've editted my orginal code - so you do your onTriggerEnter stuff and then at the end check if you have more than 10 score. If you do then the code gets run. To destroy a wall just assign the wall to a public GameObject variable like in the answer by the other guy.
Thank you this worked perfectly! $$anonymous$$y brain is fried from all of this lol.
What if you want to do something every time the coin count reaches increments of 1000?
Your answer
Follow this Question
Related Questions
Similar system to animation events? 0 Answers
Mecanim animation events not working properly 0 Answers
Difference between OnPointerEnter and OnMouseEnter 3 Answers
Generic Triggers and Actions 0 Answers
Assigning events on runtime 0 Answers