Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Pancho17 · Apr 03, 2013 at 05:10 AM · classreferenceclonecopynot

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);
 
 }
 
 
 
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pancho17 · Apr 03, 2013 at 06:34 PM 0
Share

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'

avatar image Loius · Apr 03, 2013 at 07:20 PM 0
Share

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

avatar image Pancho17 · Apr 04, 2013 at 01:44 AM 0
Share

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.

avatar image Chronos-L · Apr 04, 2013 at 01:56 AM 0
Share

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.

avatar image Pancho17 · Apr 04, 2013 at 06:26 AM 0
Share

Ill do so thanks

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pancho17 · Apr 10, 2013 at 07:39 AM 0
Share

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

avatar image Umai · Nov 27, 2014 at 03:10 AM 0
Share

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.

avatar image elseforty · May 09, 2018 at 12:16 AM 0
Share

hello the second solution will not work, i tried it , you have to copy class elements into new class instance manually ,solution 1

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges