I have a code that allows me to split balls into smaller balls if hit by a projectile. What it does is that once the two balls instantiate it check if the ball is tagged as “ball” if it is, it’ll transform the instantiate balls into smaller balls and then changes the split balls into “large ball” tag. Then it repeats until it reaches the “Small Ball” tag. What I want is instead of the tags is there a way to use variables instead? E.g. if(SizeType == “Large ball”) instead of if(gameObject.tag == “Large Ball”) if so, is there a way that allows the projectile to destroy itself if using variables? Here are the codes for both
Split Function/Ball code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball_Behavioiur : MonoBehaviour {
private GameObject player;
private Rigidbody2D rb;
public int thrust;
private CircleCollider2D circle;
public float LargeBallScale;
public float MedBallScale;
public float SmallBallScale;
public GameObject Ball;
private GameObject Projectile;
public float Ball1TranslateX;
public float Ball2TranslateX;
public float Ball1TranslateY;
public float Ball2TranslateY;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
rb = GetComponent<Rigidbody2D>();
circle = GetComponent<CircleCollider2D>();
rb.AddForce(Vector2.right * thrust);
}
// Update is called once per frame
void Update () {
circle.isTrigger = false;
}
private void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ball")
{
circle.isTrigger = true;
}
}
void HandleSplit(){
Projectile = GameObject.FindGameObjectWithTag ("Projectile");
if (gameObject.tag != "Small Ball") {
var ball1 = Instantiate (Ball);
var ball2 = Instantiate (Ball);
ball1.GetComponent<Ball_Behavioiur> ();
ball2.GetComponent<Ball_Behavioiur> ();
ball1.GetComponent<Rigidbody2D> ();
ball2.GetComponent<Rigidbody2D> ();
Vector2 temp = transform.position;
ball1.transform.position = temp;
ball1.transform.Translate (Ball1TranslateX, Ball1TranslateY, 0, Space.World);
ball2.transform.position = temp;
ball2.transform.Translate (Ball2TranslateX, Ball2TranslateY, 0, Space.World);
var Largeballscale = new Vector3 (LargeBallScale, LargeBallScale, 1);
var medballscale = new Vector3 (MedBallScale, MedBallScale, 1);
var smallballscale = new Vector3 (SmallBallScale, SmallBallScale, 1);
if (gameObject.tag == "Ball") {
ball1.transform.localScale = Largeballscale;
ball2.transform.localScale = Largeballscale;
ball1.tag = "Large Ball";
ball2.tag = "Large Ball";
}
if (gameObject.tag == "Large Ball") {
ball1.transform.localScale = medballscale;
ball2.transform.localScale = medballscale;
ball1.tag = "Medium Ball";
ball2.tag = "Medium Ball";
}
if (gameObject.tag == "Medium Ball") {
ball1.transform.localScale = smallballscale;
ball2.transform.localScale = smallballscale;
ball1.tag = "Small Ball";
ball2.tag = "Small Ball";
}
}
if (Projectile == true) {
if (gameObject.tag != "Small Ball") {
Destroy (gameObject);
}
if (gameObject.tag == "Small Ball") {
Destroy (gameObject);
}
}
}
void OnTriggerEnter2D(Collider2D blip){
HandleSplit ();
}
}
Projectile code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour {
public Vector2 Speed;
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
rb.velocity = Speed;
}
// Update is called once per frame
void Update () {
rb.velocity = Speed;
}
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.CompareTag ("Ceiling")) {
Destroy (gameObject);
}
if (target.gameObject.tag != "Small Ball") {
Destroy (gameObject);
}
if (target.gameObject.tag == "Small Ball") {
Destroy (gameObject);
}
}
}