- Home /
Collision of Character collider not working?
I wrote a script to pickup objects on collision, and subtract from a var.
var stilltofind = 6;
var leveltoload : String;
function OnControllerColliderHit (hit : ControllerColliderHit) {
if(hit.gameObject.tag == "topickup"){
Destroy(hit.gameObject);
stilltofind -= 1;
}
}
function Update () {
if(stilltofind == 0){
Application.LoadLevel(leveltoload);
}
}
When I play the game, and go to the object, the object doesn't disappear, and the var isn't subtracted from.
Can somone troubleshoot?
Attach this script to your Player.
JavaScript:
#pragma strict
var stilltofind : int = 2;
var leveltoload : String;
function OnCollisionEnter (hit : Collision) {
if(hit.gameObject.tag == "topickup"){
Destroy(hit.gameObject);
stilltofind--;
}
}
function Update () {
if(stilltofind == 0){
Application.LoadLevel(leveltoload);
}
}
It's basically what you wrote, but I tweaked it a little. I'll explain in the comments.
If this worked, please mark this answer as correct.
If it didn't work, please notify me via the comments. I will be monitoring this page until the problem is solved, or until you are absent for a time.
Happy Coding!
Noah.
Here are the basics...
$$anonymous$$ake sure this is attached to your Player
$$anonymous$$ake sure your pickup objects are tagged as whatever they are called to be in the script (Ex. "topickup")
Change the first variable (int) to the total number of pickup items in the scene.
Change the "topickip" tag to whatever you want (or keep it the way it is).
Note: I changed the ("yourVar -= 1;") to ("yourVar--"). It's easier to use a decrement for this type of thing.
If you have any issues, please ask me. If this worked, please mark this as correct.
Happy Coding!
Noah.
I relaunched Unity with no changes to the code, and the collision script worked like a charm!
I guess it was a glitch? I don't know, but it's working now. Thank you guys for all the help I got!
Answer by golemsmk · Mar 09, 2014 at 12:28 AM
Check for your others object, their Collider-Component must be a collider, NOT a trigger if you want to use OnCharacterControllerHit function.
Other way, if you must use trigger but not collider on object for some reasons, you must attach Rigibody-component to these objects and write a Script attach to them, using the OnCollisionStay() or OnCollisionEnter() function to destroy themself or do whatever you have to do.
I hope this useful, I see no error in your code. Cheer :)!
Your answer
Follow this Question
Related Questions
Why is rigidbody.AddRelativeTorque not working? 1 Answer
For statement errors? 1 Answer
For loop not working? 1 Answer
Reload Level on Collision 4 Answers
OnMouseOver parenting problem 0 Answers