- Home /
Collecting Papers and Keeping Track of how many collected - need help
Hi, having many issues. This is my code... the furthest I've gotten after a half day trying to get it to work. I have 30 papers scattered around my scene and I want the papers to be destroyed/vanish when the first person controller touches them and I'd like the 'score' to count the number collected - so far this code is only able to display the GUI as 0 Papers - which is great, but I really need help on this.
var papersDisplay : GUIText;
var paper = 0;
var paperToWin = 30;
function Start() {
DisplayAmount();
}
function DisplayAmount () {
papersDisplay.text = ""+ paper + " Papers";
}
function OnTriggerEnter( other : Collider ) {
if (other.tag == "paper") {
paper += 1;
Destroy(other.gameObject);
}
}
Answer by AlucardJay · Apr 14, 2012 at 03:20 AM
A few things to check :
the statement [other.tag == "paper"] should read [other.gameObject.tag == "paper"]
does the gameObject have the tag "paper"
does either the player or the paper have a rigidbody component? e.g. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.
is either of the object's collider's set to [is Trigger]
From the Unity Scripting Reference : http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
Edit with code from comments :
var Paper : int = 0; var paperToWin : int = 30; function Start() { } function Update() { } function OnTriggerEnter( other : Collider ) { // Debug.Log("Triggered by [tag] : " + other.gameObject.tag); // Debug.Log("Triggered by [name] : " + other.gameObject.name); if (other.gameObject.tag == "Paper"){ Paper += 1; Debug.Log("A paper was picked up. Total papers = " + Paper); Destroy(other.gameObject); } } function OnGUI() { if (Paper < paperToWin) { GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Papers"); } else { GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!"); } }
Edit : uploaded an example using this script.
Here is a link to an example : http://www.alucardj.net16.net/unityanswers/PickUpPaper.html
Here is a link to the script on the example : http://www.alucardj.net16.net/unityanswers/ScriptPickUpPaper.js
ok to check if the the trigger is working , try some Debugging.
put this as the first and second line in the OnTriggerEnter. The console should print some information when the trigger is activated. IF no printout , the trigger is not working.
Debug.Log("Triggered by [tag] : " + other.gameObject.tag);
Debug.Log("Triggered by [name] : " + other.gameObject.name);
also , try setting the collider of the paper to Is Trigger = enabled.
Tried it - Had a multiple print outs: Triggered by [tag] Untagged, as well as [name] First Person Controller, and [tag]Paper, and [name] PAPER, and [name] lst Floor 002.... so yeah, what does that mean?
that's it then =] the trigger is working , but you need to change your if statement. Note the printout shows the papers tag as Paper , not paper. (capitol P). so now :
if (other.gameObject.tag == "Paper") {
now you see that you can use a name as well as a tag to check things! Hope this helps =]
Ok, that totally was the case... gah, it's alway such a tiny error. HOWEVER! Unfortunately... while the papers now vanish, the 'score' is not increasing! Any ideas... Here's the revised code:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I need help with a companion!!! 2 Answers
Games and websites 1 Answer
Will not shoot, also my character will only move side ways. 1 Answer
preferences, load second time etc 1 Answer