I am just starting with Unity. I know some JavaScript and years ago I used c++. The script I am looking for is when the player touches a primitive for now (an imported mesh later). It disappears. I have done some search and tried a few things without success.
If the player is a rigidbody then you can use something like this on the primitive object:
private var myGO : GameObject;
function Start () {
myGO = gameObject;
}
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.tag!="Player") return; //Check that it's the player that collides else exit the function
myGO.active = false; //Disable this GameObject
}
Instead of disabling the object (if you need the script somehow later), you can also do this:
private var myRenderer : MeshRenderer;
private var myCollider : Collider;
function Start () {
myRenderer = renderer;
myCollider = collider;
}
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.tag!="Player") return; //Check that it's the player that collides else exit the function
myRenderer.enabled = false; //Disable this GameObject's renderer
myCollider.enabled = false; //Disable this GameObject's collider
}
Set the collider to trigger if you want the player to just pass through the object (if it's a pickup of some sort), otherwise you'll notice a small stutter as the rigidbody collides with the object.
Then do this instead:
private var myGO : GameObject;
function Start () {
myGO = gameObject;
}
function OnTriggerEnter (other : Collider) {
if (!other.CompareTag("Player")) return; //Check that it's the player that triggers else exit the function
myGO.active = false; //Disable this GameObject
}