Using variables instead of tags

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);
        }
    }
       
}

One way to do it is to use an enum (just add this above your class declaration):

public enum BallType
{
   Whole,
   Large,
   Medium,
   Small
}

Then add this variable to your Ball class:

public BallType type = BallType.Whole;

Then you can do checks like:

if(type == BallType.Whole)
{
   ...
   ball1.type = BallType.Large;
   ball2.type = BallType.Large;
 }

I typed in ball1.type but it only shows ball1.GetType instead of type

Oh yeah of course, ball1 and ball2 are GameObjects, not Ball_Behaviours.

Try this (at line 52 of your original code):

...
var ball1Obj = Instantiate(Ball);
var ball2Obj = Instantiate(Ball);
Ball_Behaviour ball1 = ball1Obj.GetComponent<Ball_Behaviour>();
Ball_Behaviour ball2 = ball2Obj.GetComponent<Ball_Behaviour>();
...

Be sure to replace ball1 and ball2 by ball1Obj and ball2Obj later in your code. Also, fix the typo in your class name (Ball_Behavio__i__ur to Ball_Behaviour)

Getting the error “Type ‘UnityEngine.GameObject’ does not contain a definition for ‘type’ and no extension method ‘type’ of type ‘UnityEngine.GameObject’ could be found. Are you missing an assembly reference?”

Edit: Nevermind I see where I messed up

Use ball1 and ball2 when getting the type, that’s the only place where you don’t use ball1Obj and ball2Obj.

Thanks for helping! It worked.

You’re very welcome :slight_smile: