- Home /
Item pick up script not working
Hey there. I'm new to Unity, and I'm working on my first simple game. Basically all you have to do is pick up four objects scattered around the area, and a the end screen pops up. The problem is that the script I wrote for it is not working. I attached the script to the player, but when I collide with an object, nothing happens. The counter doesn't go up, and the object isn't destroyed. The script is below, and again I'm very new to this. Don't chop my head off if I made a simple mistake. Thank you for reading, and please help me out.
#pragma strict
var count : int;
private var text : GUIText;
function OnCollisionEnter(collision : Collision)
{
if(collision.transform.tag == "Finish")
{
count += 1;
Destroy(collision.gameObject);
}
print(Time.time);
}
function Update ()
{
if(count >= 4)
{
count = -1;
var g = new GameObject();
text= g.AddComponent(GUIText);
text.text = "The End";
text.transform.position = new Vector3(0.5F,0.5F,0);
text.anchor = TextAnchor.MiddleCenter;
}
}
You should use proper tagging and formatting for your questions. I've fixed this for you to give it a better chance of being answered properly.
Have a look at my edited answer below. Character Controller only detects collisions while moving. You either have to use OnControllerColliderHit like in my answer below, or attach a rigidbody to the character.
As a suggestion you should make use of Debug.Log("Log text");
This way you can track how far your script ran before you encountered the problem.
Answer by clunk47 · Aug 06, 2013 at 04:11 AM
Be sure the tag is correct, make sure you have a rigidbody attached to one of the objects as well. Also be certain neither collider is a trigger.
From the script reference, "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.".
But if you are using a CharacterController, you need to use OnControllerColliderHit
[1]:http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
Answer by Griffo · Aug 06, 2013 at 06:32 AM
Is your "Finish" GameObjects collider "is Trigger" ticked in the inspector?
I've tried it both ways, but to be honest, I don't know if it should be.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111617im_/https://answers.unity.com/themes/thub/images/avi.jpg)