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
0
Question by WorpeX · Feb 23, 2015 at 10:53 AM · c#arraylistcopy

Copy From Array to List without reference [C#]

Been looking around for awhile for a solution to this problem and it doesn't sound like something which is simple. A lot of the answers I found online weren't working in unity, were for JS or didn't apply to my application easily. So, thought i'd ask here to see if others can help me. I'm not exactly the greatest coder, so hopefully it isn't a difficult problem.

My script is a summon monster script and the monster being summoned is added to a list of monsters that the player owns. So, I need to copy the monster from the original array into a list so that the player monster can be modified without affecting the parent monster. However, i'm struggling with finding a solution that will achieve this without the parent being modified. Here is the code I have below.

Monster is a class referenced in a previous script. MonsterArray is the full list of monsters and PlayerMonsters is the list of playermonsters. MonsterList is just a temporary list.

 public static int randomNum;
 public static int randomNum2;
 public static Monster SummonedMonster;

     public void randomizeMonster()
     {
         randomNum = Random.Range (1,100);
         List<Monster> MonsterList = new List<Monster>();
         
         
         if (randomNum <= 69)
         {
             for(int i=0;i<Main.control.MonsterArray.Length;i++)
             {
                 if(Main.control.MonsterArray[i].Rarity == Rarity.Common)
                 {
                     MonsterList.Add (Main.control.MonsterArray[i]);
                 }
             }    
         
         }
         
         if (randomNum >= 70 && randomNum <= 94)
         {
             for(int i=0;i<Main.control.MonsterArray.Length;i++)
             {
                 if(Main.control.MonsterArray[i].Rarity == Rarity.Uncommon)
                 {
                     MonsterList.Add (Main.control.MonsterArray[i]);
                 }
             }    
             
         }
         
         if (randomNum >= 95)
         {
             for(int i=0;i<Main.control.MonsterArray.Length;i++)
             {
                 if(Main.control.MonsterArray[i].Rarity == Rarity.Rare)
                 {
                     MonsterList.Add (Main.control.MonsterArray[i]);
                 }
             }    
             
         }
         
         randomNum2 = Random.Range (0,MonsterList.Count);        
         SummonedMonster = MonsterList[randomNum2];
         Main.control.PlayerMonsters.Add (SummonedMonster); 
     }


Thanks for the help!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WorpeX · Feb 23, 2015 at 12:47 PM

That worked perfectly! Thank you very much for your help!

I actually had a reason for them to be declared outside the function at one point, but I ended up splitting the script into 2 different scripts and never moved the declarations down. Have done that now, thanks again!

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 sniper43 · Feb 23, 2015 at 11:26 AM

You ddin't really specify your problem. I think you need to create a seperate monster using the "new" keyword. See 2.

  1. why are

    public static int randomNum; public static int randomNum2; public static Monster SummonedMonster;

static? Read up on the reference and decide if that's the desired behaviour. From what I can see you can easily just delcare them inside the randomizeMonster function, since all other relevant data should be in SummonedMonster

  1. SummonedMonster = MonsterList[randomNum2]; Main.control.PlayerMonsters.Add (SummonedMonster);

Should be

 SummonedMonster = new Monster(MonsterList[randomNum2]);
 Main.control.PlayerMonsters.Add (SummonedMonster);

or

 Main.control.PlayerMonsters.Add (new Monster(MonsterList[randomNum2]));

and you can omit declaring SummonedMonster.

The new keyword creates a new instance of a gameobject. Here's the relevant page: https://msdn.microsoft.com/en-us/library/k6sa6h87.aspx

You might have to modify the Monster script to support creating new objects. Example:

 public class Monster 
 {
     float paramA;
     float paramB;
     string paramC;
 }

would turn into

 public class Monster 
 {
     public float paramA;
     public float paramB;
     public string paramC;
 
     public Monster(Monster monster)
     {
         this.paramA = monster.paramA;
         this.paramB = monster.paramB;
         this.paramC = monster.paramC;
     }
 }


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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Detecting OnMouseDown from colliders in an array 1 Answer

How to make a or array of GUI.Button's? 1 Answer

Pick between two floats 2 Answers

How would I find the name of all game objects in a c# List 1 Answer

Possible shader interference when updating matrix 1 Answer


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