- Home /
Question by
defenceplox · Jul 04, 2013 at 07:18 PM ·
collisiondestroymultiple
multiple collisions to Destroy (gameObject);
i want to make it so depending on the blocktype, it takes multiple collisions to destroy without making a new script. heres what ive got on the block code.
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
public BlockType blockType = BlockType.Wood;
// Use this for initialization
void Start () {
MeshRenderer mr = GetComponent<MeshRenderer>();
mr.material = Resources.Load ( "Materials/block" + blockType.ToString() + " mat" ) as Material;
}
void OnCollisionEnter( Collision col ) {
Score.score += (int)blockType;
Destroy( gameObject );
}
}
public enum BlockType {
Wood = 10,
Stone = 25,
Brick = 50
}
Comment
Best Answer
Answer by iwaldrop · Jul 04, 2013 at 10:42 PM
Instead of calling Destroy immediately after incrementing your score, you have to decide if you want to destroy it first. To do that you should know how many hits it takes to destroy each block type. Then, each hit should increment a counter. Once that number is reached then you destroy it.
int hitsNeeded = 5;
int hitsTaken;
void OnCollisionEnter()
{
Score.score += (int)blockType;
if (++hitsTaken >= hitsNeeded)
Destroy(gameObject);
}
thanks so much. worked great. added a public to the hitsneeded for ease of access