So I came up with these two ways, one using scriptable objects, and the other one disables and enables game objects.
Method 1:
This method uses scriptable objects, I simply assign a lot of different prefabs for different objects, which I then instantiate on the designated transform of the main GameObjects, however, the code for this is very annoying to write and hard to read
Scriptable Object:
The Game Object and its children:
the attachment controller:
The scriptable object code:
using UnityEngine;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/ScriptableWeapons", order = 1)]
public class ScriptableWeapons : ScriptableObject
{
public GameObject[] BarPrefabs;
public GameObject[] HandPrefabs;
public GameObject[] HandlePrefabs;
public GameObject[] MagPrefabs;
public GameObject[] MuzzlePrefabs;
public GameObject[] ScoPrefabs;
public GameObject[] SideLPrefabs;
public GameObject[] SideRPrefabs;
public GameObject[] StockPrefabs;
public GameObject[] TriPrefabs;
public GameObject[] UbderbPrefabs;
}
The Manager Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttachementController : MonoBehaviour
{
public bool GunLevelOn = true;
public int GunLevel = 1;
public int Mag = 1;
public int Trigger = 1;
public int Scope = 1;
public int Barrel = 1;
public int Ubderbarrel = 1;
public int Muzzle = 1;
public int Stock = 1;
public int HandGuard = 1;
public int SideL = 1;
public int SideR = 1;
public int Handle = 1;
bool CanUnder = true;
public Transform MagPosition;
public Transform ScoPosition;
public Transform StockPosition;
public Transform BarPosition;
public Transform TriPosition;
public Transform UbderbPosition;
public Transform MuzzlePosition;
public Transform HandPosition;
public Transform SideLPosition;
public Transform SideRPosition;
public Transform HandlePosition;
// public Transform RealMuzzle;
/*
Mag
Tri
Sto
Sco
Bar
Ubderb
Muzzle
Hand
SideL
SideR
Handle
*/
GameObject currentMagz;
GameObject currentScopez;
GameObject currentStockz;
GameObject currentBarrelz;
GameObject currentTriggerz;
GameObject currentUbderbz;
GameObject currentMuzzlez;
GameObject currentHandz;
GameObject currentSideLz;
GameObject currentSideRz;
GameObject currentHandlez;
int currentmag;
int currentscope;
int currentstock;
int currentbarrel = 0;
int currentTri;
int currentBar;
int currentUbderb;
int currentMuzzle = 1;
int currentHand;
int currentSideL;
int currentSideR;
int currentHandle;
public ScriptableWeapons AttachementValues;
// This will be appended to the name of the created entities and increment when each is created.
// int instanceNumber = 1;
void FixedUpdate()
{ if (GunLevelOn){
Mag = GunLevel;
Scope= GunLevel;
Stock = GunLevel;
Barrel = GunLevel;
}
// currentUbderbz.SetActive(CanUnder);
Barrel = Mathf.Clamp(Barrel, 1, AttachementValues.BarPrefabs.Length);
HandGuard = Mathf.Clamp(HandGuard, 1, AttachementValues.HandPrefabs.Length);
Handle = Mathf.Clamp(Handle, 1, AttachementValues.HandlePrefabs.Length);
Mag = Mathf.Clamp(Mag, 1, AttachementValues.MagPrefabs.Length);
Muzzle = Mathf.Clamp(Muzzle, 1, AttachementValues.MuzzlePrefabs.Length);
Scope = Mathf.Clamp(Scope, 1, AttachementValues.ScoPrefabs.Length);
SideL = Mathf.Clamp(SideL, 1, AttachementValues.SideLPrefabs.Length);
SideR = Mathf.Clamp(SideR, 1, AttachementValues.SideRPrefabs.Length);
Stock = Mathf.Clamp(Stock, 1, AttachementValues.StockPrefabs.Length);
Trigger = Mathf.Clamp(Trigger, 1, AttachementValues.TriPrefabs.Length);
Ubderbarrel = Mathf.Clamp(Ubderbarrel, 1, AttachementValues.UbderbPrefabs.Length);
// Debug.Log(AttachementValues.MagPrefabs.Length);
// MuzzlePosition.transform.position = currentBarrelz.transform.GetChild(0).transform.position;
// Ubderbarrel= (Ubderbarrel + 1) % AttachementValues.UbderbPrefabs.Length;
if (currentmag != Mag)
{
Destroy(currentMagz);
currentMagz = Instantiate(AttachementValues.MagPrefabs[Mag-1], MagPosition.transform.position , MagPosition.transform.rotation, this.transform);
currentMagz.transform.parent = gameObject.transform;
currentMagz.transform.position = MagPosition.transform.position;
currentMagz.name = "currentmagz";
currentmag = Mag;
}
if (currentscope != Scope)
{
Destroy(currentScopez);
currentScopez = Instantiate(AttachementValues.ScoPrefabs[Scope-1], ScoPosition.transform.position , ScoPosition.transform.rotation, this.transform);
currentScopez.transform.parent = gameObject.transform;
currentScopez.transform.position = ScoPosition.transform.position;
currentScopez.name = "currentscopez";
currentscope = Scope;
}
if (currentstock != Stock)
{
Destroy(currentStockz);
currentStockz = Instantiate(AttachementValues.StockPrefabs[Stock-1], StockPosition.transform.position , StockPosition.transform.rotation, this.transform);
currentStockz.transform.parent = gameObject.transform;
currentStockz.transform.position = StockPosition.transform.position;
currentStockz.name = "currentstockz";
currentstock = Stock;
}
if (currentbarrel != Barrel)
{
Destroy(currentBarrelz);
currentBarrelz = Instantiate(AttachementValues.BarPrefabs[Barrel-1], BarPosition.transform.position , BarPosition.transform.rotation, this.transform);
currentBarrelz.transform.parent = gameObject.transform;
currentBarrelz.transform.position = BarPosition.transform.position;
currentBarrelz.name = "currentbarrelz";
currentbarrel = Barrel;
MuzzlePosition.position = currentBarrelz.transform.GetChild(0).transform.position;
currentMuzzlez.transform.position = MuzzlePosition.position;
}
if (currentMuzzle != Muzzle)
{
Destroy(currentMuzzlez);
currentMuzzlez = Instantiate(AttachementValues.MuzzlePrefabs[Muzzle-1], MuzzlePosition.transform.position , MuzzlePosition.transform.rotation, this.transform);
currentMuzzlez.transform.parent = gameObject.transform;
currentMuzzlez.transform.position = MuzzlePosition.transform.position;
currentMuzzlez.name = "currentmuzzlez";
currentMuzzle = Muzzle;
}
if (currentHand != HandGuard)
{
if(HandGuard >= 5) {
CanUnder = false;
}
else{
CanUnder = true;
}
Destroy(currentHandz);
currentHandz = Instantiate(AttachementValues.HandPrefabs[HandGuard-1], HandPosition.transform.position , HandPosition.transform.rotation, this.transform);
currentHandz.transform.parent = gameObject.transform;
currentHandz.transform.position = HandPosition.transform.position;
currentHandz.name = "currentHandz";
currentHand = HandGuard;
if(CanUnder){
UbderbPosition.position = currentHandz.transform.GetChild(0).transform.position;
currentUbderbz.transform.position = UbderbPosition.position;
SideRPosition.position = currentHandz.transform.GetChild(1).transform.position;
currentSideRz.transform.position = SideRPosition.position;
SideRPosition.rotation = currentHandz.transform.GetChild(1).transform.rotation;
currentSideRz.transform.rotation = SideRPosition.rotation;
SideLPosition.position = currentHandz.transform.GetChild(2).transform.position;
currentSideLz.transform.position = SideLPosition.position;
SideLPosition.rotation = currentHandz.transform.GetChild(2).transform.rotation;
currentSideLz.transform.rotation = SideLPosition.rotation;
}
}
if (currentHandle != Handle)
{
Destroy(currentHandlez);
currentHandlez = Instantiate(AttachementValues.HandlePrefabs[Handle-1], HandlePosition.transform.position , HandlePosition.transform.rotation, this.transform);
currentHandlez.transform.parent = gameObject.transform;
currentHandlez.transform.position = HandlePosition.transform.position;
currentHandlez.name = "currentHandlez";
currentHandle = Handle;
}
if (currentSideL != SideL)
{
Destroy(currentSideLz);
currentSideLz = Instantiate(AttachementValues.SideLPrefabs[SideL-1], SideLPosition.transform.position , SideLPosition.transform.rotation, this.transform);
currentSideLz.transform.parent = gameObject.transform;
currentSideLz.transform.position = SideLPosition.transform.position;
currentSideLz.name = "currentSideL";
currentSideL = SideL;
}
if (currentSideR != SideR)
{
Destroy(currentSideRz);
currentSideRz = Instantiate(AttachementValues.SideRPrefabs[SideR-1], SideRPosition.transform.position , SideRPosition.transform.rotation, this.transform);
currentSideRz.transform.parent = gameObject.transform;
currentSideRz.transform.position = SideRPosition.transform.position;
currentSideRz.name = "currentSideR";
currentSideR = SideR;
}
if (currentTri != Trigger)
{
Destroy(currentTriggerz);
currentTriggerz = Instantiate(AttachementValues.TriPrefabs[Trigger-1], TriPosition.transform.position , TriPosition.transform.rotation, this.transform);
currentTriggerz.transform.parent = gameObject.transform;
currentTriggerz.transform.position = TriPosition.transform.position;
currentTriggerz.name = "currentTrigger";
currentTri = Trigger;
}
if (currentUbderb != Ubderbarrel)
{
Destroy(currentUbderbz);
currentUbderbz = Instantiate(AttachementValues.UbderbPrefabs[Ubderbarrel-1], UbderbPosition.transform.position , UbderbPosition.transform.rotation, this.transform);
currentUbderbz.transform.parent = gameObject.transform;
currentUbderbz.transform.position = UbderbPosition.transform.position;
currentUbderbz.name = "currentUnderb";
currentUbderb= Ubderbarrel;
}
currentUbderbz.SetActive(CanUnder);
currentSideRz.SetActive(CanUnder);
currentSideLz.SetActive(CanUnder);
}
}
in the second method, I simply add all the possible to the game object and enable disable them on demand:
I have not written code for this one however, since I want to firstly know is there an option of improving the first method.