- Home /
Question by
Razputin · Nov 27, 2014 at 08:09 PM ·
collisiondestroyplayerprefsdetectiondestroy object
Collision Detection not working?
Trying to make it so when the player collides with the Jetpack item it sets the playerpref for the jetpack to 1, then destroys the jetpack. But I can't seem to make the jetpack disappear.
OnTriggerEnter would be fine too, I just need the jetpack to disappear.
using UnityEngine;
using System.Collections;
public class JetpackPickup : MonoBehaviour {
public int doIHaveJetpack = 0;
void OnCollisionEnter(Collision other) {
if(other.collider.name == "Player")
{
PlayerPrefs.SetInt("doIHaveJetPack", 1);
Destroy(other.gameObject);
}
}
}
Comment
^make this an answer so I can give you best answer. I put it on the player as you suggested, but whats wrong with putting it on the object, out of curiosity.
That's a choice really, but i find it difficult to manage.
Best Answer
Answer by koray1396 · Nov 27, 2014 at 08:34 PM
You are trying to destroy the player, not the jetpack.
If this script is attached to the player, it should be;
void OnCollisionEnter(Collision other) {
if(other.collider.name == "Jetpack")
{
PlayerPrefs.SetInt("doIHaveJetPack", 1);
Destroy(other.gameObject);
}
}
if it is attached to the jetpack, would not be a good idea, but then you should do;
void OnCollisionEnter(Collision other) {
if(other.collider.name == "Player")
{
PlayerPrefs.SetInt("doIHaveJetPack", 1);
Destroy(gameObject);
}
}