- Home /
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[i].Level != 0){
i++;
}
Inventory.PokeBons.SetValue((this.gameObject.GetComponent(PokeBonList).PokeBonlist[ID]),i);
if(Inventory.PokeBons[i].Level == 0){
Inventory.PokeBons[i].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);
}
So i tried using StackExchange as Loius said but i cant seem to get it. I created a C# file and copied the whole code in it, then i accesed the inventory object and used the .clone() method we just created. It seems the method isnt recognizable by the class. i mean its like it hasnt implemented it .
'Clone' is not a member of 'PokeBon'
Pretty sure I've used the this parameter keyword in Unity, but it was either rather tricky or impossible and I didn't use it. It's definitely easier to do without this, calling newObject = ObjectCopier.Clone(targetObject);
Well i tried to put the script onto an object but it says i cant do it because the script class cant be abstract... Though in the code theres not a single abstract word in the whole file. What does this mean? any clue? THanks for the info up til now.
Seeing that you have put in this much efforts, you should include your script in the question and we will see if we can give you the final push to make this work.
Answer by fafase · Apr 10, 2013 at 06:23 AM
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.
Hey! thank you soooo much, ill try and make that tomorrow , ill tell you if it works! Though your idea seems to be 100% logic so it should work :D
If memberA and memberB are class/objects, then this will be a shallow copy, so both Pokebons will share the same memberA/B objects in memory.
hello the second solution will not work, i tried it , you have to copy class elements into new class instance manually ,solution 1
Answer by Loius · Apr 03, 2013 at 06:33 AM
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.
Answer by nuverian · Apr 03, 2013 at 06:44 AM
If your classes are of MonoBehaviour, then you can just Instantiate the class. Sorry if they are not, I guess Loius is right
Answer by originalterrox · Jan 07, 2019 at 01:04 AM
Example of cloning MonoBehaviour values instead of making a reference.
SOMETHING newItem = new SOMETHING();
newItem = UnityEngine.Object.Instantiate (copyingfrom.gameObject.GetComponent<SOMETHING >());
Your answer
Follow this Question
Related Questions
Can I get a reference (not a copy) to a string from a script? 2 Answers
Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers
NullReferenceException was thrown. Object reference not set to an instance of an object. 1 Answer
UnityScript - Class reference 1 Answer
Object reference not set to an instance of an object. 0 Answers