Hello. Just wondering if I could get some help solving a coding issue. I’m attempting to build a script that destroys a tagged prefab in the scene on collision with a ball and then automatically decreases the number of that tagged prefabs remaining in a counter. This is the coding I’ve got so far. Fair warning, it’ll be messy, I’m not experienced with that much coding and this is using an old version of Unity. (Can’t download new version until I finish the project before anyone asks).
[C#]
using UnityEngine;
using System.Collections;
public class TinCanDestroyed : MonoBehaviour {
public int Tin Cans;
void OnGUI(){
GUI.Label (new Rect(5, 105, 105, 23), "Tin Cans : " + Tin Cans);
void Update () {
OnCollisionEnter(collision : Collision){
if(theCollision.gameObject.tag == “TinCan”){
Destroy(gameObject);
}
if(theCollision.gameObject.tag == “TinCan”) && Tin Cans > 0)
A couple of things. The best way to destroy an object when hit is to use the OnTriggerEnter(Collider other) on the prefab itself. So… create a script to go on the prefab and give it a method similar to this.
Instead of destroying the other object like they do in the API. Simply check to see if the other collider is a ball and if it is then you can call Destroy(gameObject);
now for the counter. I would make an ArrayList of the prefabs in the TinCanDestroyed class. And instead of using a counter, just call the ArrayList Count() method.
One more thing. Please do not put spaces in your variable names. That hurts my eyeballs.