Clone class variables to another... Copy class not by reference

Hello, so im making this pokemon clone.
I created a class pokebon which holds every stat.
Then i created a variable called pokebon list of type pokebon which holds every single pokebon stats.
The problem is when i make the character get a pokebon. I copy the class to another one in the objects inventory. The class isnt copied, but its sent as reference, so each time it levels up or changes any attribute the pokebon list which holds the base info changes too!

Is there a possibility to transfer the variables of a class to another without passing it as reference and without handwriting every single variable to another??

THIS IS THE CODE WHERE I CREATED THE POKEBON CLASS AND ALSO WHERE I ADD POKEBONS TO THE INVENTORY

#pragma strict

var LastPosition:Vector3 = Vector3.zero;
var LastOneWasDoor:boolean = false;
var ChatBoxProgress:boolean[];
var Inventory:InventoryClass;
var Sounds:AudioClip[];
var BattleInfo:BattleInfoClass;



class InventoryClass{
	var Item:item[];
	var Money:int;
	var PokeBons:PokeBon[];

};

class BattleInfoClass{
	var CharacterID:int;
	var CharacterID2:int;
	var WildBattle:boolean;
	var RivalPokeBon:int;
	var RivalLVL:int;
};

class item{
	var Name:String;
	var Quantity:int;
	var Price:int;
	var Usable:boolean;
	var ID:int;
};


class PokeBon{
	var ID:int;
	var Pokebon:GameObject;
	var Health:int;
	var Mana:int;
	var Level:int;
	var Experience:int;
	var Name:String;
	var Strength:int;
	var Dexterity:int;
	var MagicDamage:int;
	var AgileBoost:boolean;
	var Stab:boolean;
	var SpitAcid:boolean;
	var FrostBolt:boolean;
	var FireBolt:boolean;
	var Tornado:boolean;
	
	public function ReturnPokeBon(){
		return this;
		
	}

};


function Start () {
	DontDestroyOnLoad(this.gameObject);
}

function Awake(){
	for(var ChatBox in GameObject.FindGameObjectsWithTag("Chat")){
		if(ChatBoxProgress[ChatBox.GetComponent(TriggerChatBox).ChatBoxN]){
			ChatBox.GetComponent(TriggerChatBox).Triggered = true;
		}
	}
	
}

function Update () {

}

function GetLastPosition(){
	if(LastPosition != null){
		return LastPosition;
	}else{
		return Vector3.zero;
	}

}

function SetLastPosition(Door:boolean,Pos:Vector3){
	
	
	LastPosition = Pos;
	
	Pos += Vector3.forward *0.3;
	
}


function TriggerChat(ChatNum:int){
	ChatBoxProgress[ChatNum] = true;
	print("DEACTIVATED CHATBOX");
	
}


function AddPokeBon(ID:int){
	var i:int = 0;
	while(Inventory.PokeBons*.Level != 0){*
  •  i++;*
    
  • }*
  • Inventory.PokeBons.SetValue((this.gameObject.GetComponent(PokeBonList).PokeBonlist[ID]),i);*
    _ if(Inventory.PokeBons*.Level == 0){_
    _ Inventory.PokeBons.Level = 1;
    }*_

}

function InitializeBattle(WildBattle:boolean,RivalPokemon:int,LVL:int){
* if(WildBattle){*
* BattleInfo.CharacterID = -1;*
* BattleInfo.CharacterID2=-1;*
* BattleInfo.WildBattle=true;*
* BattleInfo.RivalLVL = LVL;*
* BattleInfo.RivalPokeBon = RivalPokemon;*
* Application.LoadLevel(“Fight”);*

* }*

}

function InitializaBattle(CharacterID:int,CharacterID2:int){

}

function ReturnBattleInfo(){
* return (BattleInfo);*

}

If I get it right you want to clone your object. That is if you have a Pokebon pikatchu you want a pikatchu2 with all the values but of its own meaning it is not just a reference to it but a totally new object.

Two options, either you need to create a function that performs a deep copy meaning something like this:

public static Pokebon DeepCopy(PokeBon p){
   Pokebon temp = new Pokebon();
   temp.memberA = p.memberA;
   temp.memberB = p.memberB;
   // and so on
   return temp;
}

and you use it:

PokeBon existingPokebon = new PokeBon();
PokeBon newOne =  PokeBon.DeepCopy(existingPokebon);  

other solution is to use structure instead since structure are passed by value

PokeBon existingPokeBon = new PokeBon();
PokeBon newOne = existingPokeBon;

The second line will copy the orginal one into the new one.

Actually, depending on how crazy you want to get, StackExchange seems to have just about every option listed out nicely.

Having a custom copy constructor is often a good solution so you can pick and choose which member objects are deep-copied and which are just shallow.

If your classes are of MonoBehaviour, then you can just Instantiate the class. Sorry if they are not, I guess Loius is right

Example of cloning MonoBehaviour values instead of making a reference.

SOMETHING newItem = new SOMETHING();
newItem =  UnityEngine.Object.Instantiate (copyingfrom.gameObject.GetComponent<SOMETHING >());