Collider Script Not Detecting

Hello!

I’ve got this script to detect whether the player has picked up an item, and heres the deal: I have a paper entity I imported, and I added a sphere collider, to it, and if the box is trigger is not checked, nothing happens, and the player can move through the paper, and if the is trigger box is checked, the paper never appears, and it says I have one paper. Im new to unity, so forgive some silly mistakes :slight_smile:
var Paper : int = 0;
var paperToWin : int = 8;

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 == "dirt"){
       Paper += 1;
       Debug.Log(Paper + "/8 Collected");
       Destroy(other.gameObject);
    //}
}
 
function OnGUI()
{
    if (Paper < paperToWin)
    {
       GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + "/8 Collected");
    }
    else
    {
       GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
    }
}

EDIT: I forgot to mention!
The script is a child of the first person controller!

3 Answers

3

About OnTriggerEnter, documentation says:

This message is sent to the trigger collider and the rigidbody (if any) that the trigger collider belongs to, and to the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

As you say, you’re attaching that script to a child. Try to attach it to the parent, which should has a rigidbody.

Fast references:

cant use functions inside a function, move the function on trigger enter to the end of the script outside the update function and use

function Update ()
{
OnTriggerEnter();
}

I dont understand what you mean. Im pretty sure my OnTriggerEnter function isnt inside the update function

How do I attach a rigidbody to the first person cam controller, and isnt the 1st person cam a parent of the script?

Actually that's not the best approach for your set-up as I see. If you want a movable character to pick some objects around, you can follow the technique described here: http://answers.unity3d.com/questions/273419/picking-up-a-box.html

Thats not quite what I was looking for. Aren't I able to do it with this script and a first person controller?

function OnCollisionEnter(collision : Collision) { for ( var contact : ContactPoint in collision.contacts ) { if (contact.otherCollider.name == "Paper"){ //blahhh } } } edit can use .tag or . name

Could you post a tutorial? I can't seem to get it working :(